Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow "/" forward slash in regular expression

var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
console.log(patt.test("\u002f"));

I know that u002f is a forward slash in Unicode. I've tried adding that to the pattern as well as "/" and haven't been able to get it to log true yet.

like image 241
webdev5 Avatar asked Jul 22 '15 14:07

webdev5


2 Answers

You can escape a / character, by using \/.

Using unicode will actually result in the absolute same result, as using the character itself - and therefore will not solve your problem.

like image 64
slartidan Avatar answered Oct 21 '22 14:10

slartidan


It is easy to add a forward slash, just escape it. No need using any character references or entities.

var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[\/a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
                                                    ^                 

var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[\/a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
alert(patt.test("/test"));
like image 44
Wiktor Stribiżew Avatar answered Oct 21 '22 15:10

Wiktor Stribiżew