问题描述:

I have a HTML file that is used to produce a PDF and gets a date from a previous page - screen selection.

It is about string manipulation (extraction a substring that starts with “[“ from a string and displaying the result).

Example: instead of "Van4[15]" it is supposed to display "Van4".

[RoomName]=Van4[15]

The HTML file is:

<html>

<head>

<script>

function extract() {

var str = "van6[15]";

return str.substring(0, str.lastIndexOf("["));

}

document.getElementById("demo").innerHTML = extract();

</script>

</head>

<body onload="extract()">

<table>

<tr>

<td colspan="2" style="text-align:center; font-size:60; font-family:Verdana; font-weight:bold;border:1px solid Black;">

<p id="demo"></p>

</td>

</tr>

</table>

</body>

</html>

网友答案:

You could use the replace method on the string object using RegEx. I'm not sure if there's any variation, but for the example you gave in the initial post I think this should help:

var mystring = '[RoomName]=Van4[15]';
var myNewString = mystring.replace(`/.+=(.+)\[.+/g`, '$1');

console.log(myNewString);

https://jsfiddle.net/ejzg32jr/

相关阅读:
Top