I have problem with regex /!!(\w*)!!(?!\w)/g
is matching me backwards because I needed check it from end to beggining. I will explain on examples whats wrong:
examples:
!!foo!!bar!!
- should match only bar
, works correct
!!foo!!!!bar!!
- should match foo
and bar
, works correct
!!foo!!!!bar!!test
- should match foo
and bar
, works not correct now only matching foo
CodePudding user response:
you could create a reverseString function like
function reverseString(str) {
return str.split("").reverse().join("");
}
now if you apply reverseString on your input (and the matcher result) you can go along with the simple RegExp !!\w !!
CodePudding user response:
Use Regex: /!!(\w*)!!/g
var st = ['!!foo!!bar!!', '!!foo!!!!bar!!', '!!foo!!!!bar!!test'];
st[0].split("").reverse().join("").match(/!!(\w*)!!/g).map(x => x.split("").reverse().join("").replace(/!/g, '')).reverse() // ['bar']
st[1].split("").reverse().join("").match(/!!(\w*)!!/g).map(x => x.split("").reverse().join("").replace(/!/g, '')).reverse() // ['foo', 'bar']
st[2].split("").reverse().join("").match(/!!(\w*)!!/g).map(x => x.split("").reverse().join("").replace(/!/g, '')).reverse() // ['foo', 'bar']
or
st.map(y => y.split("").reverse().join("").match(/!!(\w*)!!/g).map(x => x.split("").reverse().join("").replace(/!/g, ''))).map(z => z.reverse())
// [['bar'], ['foo', 'bar'], ['foo', 'bar']]