Regexp - negative match for fixed string (vs. a char list)?

M

Mark Anderson

Is there a way in a regexp to *not* match a fixed string value?

Using [^blah] gives matches to anything not containing *any* of letters
b,l,a and h. Whereas I want to match anything that does not containing
the exact string 'blah', i.e. *all* the letters.

Possible?

Thanks

Mark
 
N

Noah Sussman

I want to match anything that does not containing
the exact string 'blah', i.e. *all* the letters.

var myString = "sometimes blah is blah";
var myMatch = myString.replace('blah', '');

Now myMatch contains your original string, except for the parts that
matched "blah."

If that doesn't solve your problem, please provide example code that
includes the regex you are trying to fix, and an example of the kind
of string you want to process.
 
M

Mark Anderson

Noah Sussman said:
of course I meant

var myMatch = myString.replace(/blah/g, '');

Thanks, though it doesn't quite work for what I want...

var myString = "the road to blah is long"
var myMatch = myString.replace(/blah/g, '');
alert(myMatch);
myString = "the road to there is long"
var myMatch = myString.replace(/blah/g, '');
alert(myMatch);
// which is a match?

....but you got me thinking down a new line. In Perl you can set a var =
!regexp to flip the match. So, I can iterate an array of matches and in
the loop look for :

// 'myArray' holds the target strings
// myRe holds the regexp
for (1=0;i<myArray.length;i=i+1) {
if (!re.test(myArray) { //
//do stuff

or for a fixed string just...

// 'myArray' holds the target strings
for (1=0;i<myArray.length;i=i+1) {
if (myArray !== 'blah') { //
//do stuff

My mistake was to try and construct a 'negative' test rather than look
for the failure of a positive one.

Thanks for the nudge!

Mark
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Wed, 28 Mar 2007 13:01:36, Mark Anderson <[email protected].
uk> posted:
Is there a way in a regexp to *not* match a fixed string value?

There's probably something better if you program only for more recent
browsers. But you can always do a global replace of the fixed string by
a peculiar character - \u263C occurs rarely in English text - then do a
not-match on that character, then substitute back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top