sommeguyy sommeguyy - 7 days ago 6
Javascript Question

how to not select lookbehind word in javascript regex

My regex: <br>![a-z]+(?=<br>)

fooword1<br>!fooword2<br>fooword3


I am trying to get word that begins with
"!"
, like
"!fooword2"
and exists between
<br>
tags. But when i apply this
<br>![a-z]+(?=<br>)
, i get
"<br>!fooword2".
I just need to select
"!fooword2"
.I mean i don't need to select first
<br>
.

So what is wrong with my regex?

Answer

Since JS does not support LookBehind, You could use groups to exclude the first <br>

var String = "fooword1<br>!fooword2<br>fooword3";
var Regexp = /<br>(![a-z]+\d)<br>/g;
var match = Regexp.exec(String);
console.log(match[1])

Another option will be using Positive Lookahead at the end with <br>, and relying on the ! at the beginning:

![a-z]+\d(?=<br>)

Demo: https://regex101.com/r/kRzoQB/3

Comments