Michael
Michael

Reputation: 117

need an regular expressions

Please help I need an regular expressions that can get any other string except specific one, for example I set the specific one to be "abc", then any other string like "bac", "cbaad", etc can be accept, but if "abc", no.

hope that make sense, thank you.

Upvotes: 0

Views: 61

Answers (4)

Andrew Clark
Andrew Clark

Reputation: 208455

As other answers have pointed out there is no need for regex in your simple example, but here is a regex that will do the job just in case you dumbed down your example too much and you actually have a need for something like this.

^(?!abc$).*

And if abc can't occur anywhere in the string:

^(?!.*abc).*

Upvotes: 3

o12
o12

Reputation: 465

If "abc" should not occur anywhere in the string,

var str = "bcabcd"; 
var noNoStr = "abc";
var pattern = new RegExp(noNoStr);
var result = !pattern.test(str);

Upvotes: 2

KoolKabin
KoolKabin

Reputation: 17653

if you want to exclude "abc" only why not do simple comparision... x == 'abc'

i.e

if( str == 'abc')
{
...
}else{
...
}

Upvotes: 0

Chinmayee G
Chinmayee G

Reputation: 8117

If your specific string is a single string i.e. on abc then you can do it using simple if condition,

if(myStr != "abc")
{
    // check myStr with regular expression of required pattern
}

Upvotes: 5

Related Questions