I have a strings like SHM{GHT}
and need to get a value from brackets (GHT
in this example).
I use RegExp to get it and everything worked fine before I open it in IE. My page broke here and I got an error unexpected quantifier
.
Here is my function
JavaScript
x
5
1
const getValueFromBrackets = (el): string => {
2
const valueFromBrackets = el.match(/(?<={).+?(?=})/g);
3
return valueFromBrackets[0];
4
}
5
I checked on CanIUse .match()
should work fine, something wrong with inside part /(?<={).+?(?=})/g
.
Any idea how can I rewrite it?
Would be really grateful for help!
Advertisement
Answer
The problem is with the lookbehind assertion <={
, which is not supported in old engines. As a workaround, match {(...)}
and take the first group:
JavaScript
1
1
1
console.log('SHM{GHT}'.match(/{(.+)}/)[1])