Skip to content Skip to sidebar Skip to footer

Regex Pattern Not Work Like Expect

I have pattern /[^!]!(\z|[^!])/ And i have string: 'My string here!' i need check if string have 1 exclamation (but not !! or !!!)

Solution 1:

Here is an example if I understood your question correctly

\A([^!]*)[!]{1}\Z

Solution 2:

What about this ? It should work

/\w+!{1}$/i

Solution 3:

You can use RegExp/!(?=!)/ to match ! followed by !, ! operator, RegExp.prototype.test()

var str = "My string here!";
var re = /!(?=!)/;
console.log(!re.test(str));

var str = "My string here!!";
console.log(!re.test(str));

Solution 4:

how about this

/[^!]+!$/

your patterm:

/[^!]!(\z|[^!])/

means "first character is not !, second character is !, third character, should be last character or any character which is not !"

Solution 5:

I founded what i needed. Because i need check not only end but also and middle of string

"My! string" and "My string!"

/([^!]![^!])|([^!]!$)/

Post a Comment for "Regex Pattern Not Work Like Expect"