tone
tone

Reputation: 1555

allow quotes in regex

This is my javascript Regex for matching only Latin Or Hebrew characthers:

/^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|[\w ]+ )$/i

what term i need to add in order to allow quotes? for example if the input is: abc"d. it should pass the regex test without alerting.

UPDATE

i thought it will be better if i give you the full code:

var RE_MY_REGEX = /^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|[\w ]+ )$/i,
    myValue = 'abc"d';
if (!RE_MY_REGEX.test(myValue )) {
        alert("failed");
}
else
{
 alert("success");
}

Upvotes: 0

Views: 433

Answers (2)

sergio
sergio

Reputation: 69027

You can use the quotes (") in the brackets:

/^(?:[\u0590-\u05FF\uFB1D-\uFB40 "]+|[\w "]+ )$/i

In any case, I have a doubt about the last space char in your regex... should not it be like this:

/^(?:[\u0590-\u05FF\uFB1D-\uFB40 "]+|[\w "]+)$/i

If the quotes can appear in the Hebrew text, you should put the quotes there also. I am not sure if when writing Hebrew exactly the same quotes are used as when writing latin or what the unicode for them can be. So, you could check this beforehand in your Hebrew text.

If the regex above does not work, I would suggest to go step by step: simplify it and check it with a latin-only string; then modify the regex to only consider Hebrew and check it; without and with quotes, etc...

Upvotes: 1

anubhava
anubhava

Reputation: 784868

You have an extra space before closing parenthesis ). Following code is working fine for me and alerting with "success".

var RE_MY_REGEX = /^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|["\w ]+)$/i;
myValue = 'abc"d';
if (!RE_MY_REGEX.test(myValue)) {
   alert("failed");
}
else {
   alert("success");
}

Upvotes: 0

Related Questions