I’m using RegExp to extract specific words from a string like the following:
<div class = ''></div>class class
My current RegExp statement is this:
/(<)[^(>)]*(;| |'|"e;)(class)?( |'|"e;|=)/gi
In this instance I wan t to match the word ‘class’ but only if it has specific characters (or ampersands) before and after it. This statement matches:
<div class
out of the original string, I’m using this in a replace method (to insert text where ‘class’ was) and I want to keep the other character around it, is there any way of trimming this match down to only select(replace) the word ‘class’ (in this instance)?
Or is there a better way of doing this?
Thanks in advance.
You can use next code to do this job:
But you must ensure what in
before,afterandremovedstrings does not contained special RegExp format characters (such as.[]?*and others, see MDN RegExp). Otherwise you must first escape these special characters inbefore,afterandremovedstrings.UPDATE:
You also can use valid regular expression for
before,afterandremovedto make more flexible search. For example next code remove letters ‘X’, ‘V’ and ‘Z’ but not remove ‘x’, ‘v’ and ‘z’:If you need to replace substring with another string you can use next function: