RegExp: how NOT to match string??
Trying to do exclusions with regular expressions alone can get very
messy, as you have discovered. You're better off using good
old-fashioned string manipulation techniques, as shown below.
var testString = "CDABCDERCDHJLCDPOCDABCDTPCDAKCDE";
var arrX = testString.split("CD");
var arrY = new Array();
var y = 0;
for(var x = 0; x < arrX.length - 1; x++)
if(!/AB$/.test(arrX[x])) arrY[y++] = arrX[x] + "CD";
alert(arrY.join('\n'));
: Hi all,
:
: I know how you can match a string with regular expressions, but how
: do you match anything EXCEPT some string?
:
: Example: I want to match all occurences of "CD" except where it is
: preceded by "AB". This means I want to match "XXCD", "AXCD"
: and "XBCD", but not "ABCD".
:
: If I want "CD" except when preceded by the single character "B",
: I could use /[^B]CD/g but for multiple characters /[^A][^B]CD/g
: would not work, as this also wouldn't match "AXCD" or "XBCD".
:
: I could try /[^A].CD | .[^B]CD/g or shorter /([^A]. | .[^B]) CD/g ,
: (spaces added for readability)
: but for longer preceding strings this would lead to a rather long
: specification.
:
: Are there any better methods?
:
:
: Thanks for any help,
:
: Hans Kesting
:
:
: