Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regular expression to find single or double quote not working

I have a regular expression to return false if special characters are found. I'm trying to modify it to do the same if any single or double quotes are found. This is one time that regexr.com is not helping.

Here is my expression that works for special characters:

^(?=.*?[A-Z]{2})((?!!|@|$|%|\^|&|\*)).)*$

Here is my regular expression for single and double quotes:

^(?=.*?[A-Z]{2})((?!'|").)*$

I even tried escaping them:

^(?=.*?[A-Z]{2})((?!\'|\").)*$

Please help! I've wasted too much time on this and cannot quickly figure it out.

I have a method:

var isValidText = function (val) {
    var rx = new RegExp(\^(?=.*?[A-Z]{2})((?!!|@|$|%||^|&||*)).)*$\);
    var result = rx.text(val);
    return result;
}

Simple input:

We're having a party and my house this weekend. Please bring as many friends as you like; the more the merrier.

This paragraph should be invalid as soon as it finds the single quote in We're.

like image 888
Patricia Avatar asked Sep 15 '17 17:09

Patricia


1 Answers

There's no need for lookaround here, and since you're only matching single characters you can simply use a character set instead of |.

.*[!@$%^&*'"].*

like image 200
CAustin Avatar answered Sep 28 '22 18:09

CAustin