I want to be able to retrieve !$£ from this string: (?i).*!$abc£
I have written the following Regex expression:
`(?<=\(\?i\)..)[^a-zA-Z0-9\s:]*
but for some reason it only matches !$ from the string despite if the match is seperated from the lookahead it matches perfectly.
how do i fix this?
this is javascript regex
CodePudding user response:
You can use
text = text.replace(/\(\?i\)..([^\w\s:]*)\w (\W*)/, '$1$2')
// or even
text = text.replace(/\(\?i\)..([^\w\s:]*)\w /, '$1')
See the regex demo #1 / regex demo #2
Details:
\(\?i\)- a(?i)string..- any two chars other than line break chars([^\w\s:]*)- Group 1 ($1): zero or more chars other than word, whitespace and:chars\w- one or more word chars(\W*)- Group 2: zero or more non-word chars.
The replacement is the concatenation of Group 1 and Group 2.
CodePudding user response:
You may use this replace code in javascript with a regex that matches ($i).. and grabs remaining non-whitespace string in a capture group. Then we use a replacer that removes all word characters from the captured group value.
var s = '(?i).*!$abc£aa$bb'
var r = s.replace(/\(\?i\)..(\S )/, (m, g1) => {
return g1.replace(/\w /g, '')});
console.log(r)
//=> !$£$
CodePudding user response:
/[^!$£]/g
Replace everything but !, $, and £.
const str = `(?i).*!$abc£`;
const rgx = /[^!$£]/g;
const res = str.replaceAll(rgx, '');
console.log(res);
