How can I get only the text inside Material[amhere]
?
For example, from...
PROD_RULE0001:WARNING: Metric[amhere] exceeded the UPPER WARNING limit[80.0]
... I want only the amhere
.
I tried:
var strg = "WARNING: Material[amhere] exceeded the UPPER WARNING limit[80.0]";
var testRE = strg.match("Material\[(.*)\]");
alert(testRE[1]);
strg.match(/Material\[(.*?)\]/);
The ? after the * makes it lazy, so the . does not capture everything afterwards.
another way maybe you want to use;
var material="Material[";
var str="WARNING: Material[amhere] exceeded the UPPER WARNING limit[80.0]";
var n=str.indexOf(material);
var amhere=str.substring(n+material.length, str.length).split("]")[0];
Your .*
expression is greedy and therefore eats its way into the closing ]
rather than matching up to it.
In addition to @Telémako's solution, another way is to make the expression more strict by saying "match anything except ]
. This will also solve the problem.
strg.match(/Material\[([^\]*)\]]/);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.