Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regex: Match between ']' and ' - ', ERROR: invalid quantifier

I have got this string:

var str = "[RANKING] Bank President - Main office."

The AIM:

The text between the ']' and ' - ' (notice the spaces) is to be matched in Javascript.

Effort:

So far,

1) I have tried

(?<=\])(.+)(?= -)/gi

This works Perfectly here: http://regexr.com?37abq, however, in javascript I get an Error: invalid quantifier in firebug.

2) I have also tried:

\](.+) -

This works pretty well here: http://www.rexfiddle.net/TzsXnjQ except that it matches even the ']' and the ' - ', hence, rendering it non-essential to some extents.

3) And Adding something like:

\]^(.+)$ -

The code goes dormant...

Any suggestion is highly appreciated.

like image 444
ErickBest Avatar asked Dec 02 '25 12:12

ErickBest


1 Answers

Option #2 should work, just get the second match index, eg

var str = "[RANKING] Bank President - Main office."
var matches = str.match(/\](.+?) - /);
if (matches) {
    var stringYouWant = matches[1];
}

Option #1 doesn't work because the RegExp library in JS does not support look-behind

like image 137
Phil Avatar answered Dec 05 '25 02:12

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!