Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unterminated regular expression literal js error

I have added jQuery validator add method for the presence of http check in url field.

JS

jQuery.validator.addMethod("valid_url", function(value, element) {
 if(!/^(https?|ftp):\/\/i.test(val))
    val = 'http://'+val; // set both the value
    $(elem).val(val); // also update the form element
 }

My Console throws
"unterminated regular expression literal" error in the following line.

if(!/^(https?|ftp):\/\/i.test(val))

What my mistake is?

like image 513
Sam Avatar asked Nov 09 '13 06:11

Sam


2 Answers

Regular expression literals should be surrounded by the delimiters(/). There's no terminating delimiter:

/^(https?|ftp):\/\//i
//                 ^

For example:

>> /^(https?|ftp):\/\//i.test('http://stackoverflow.com/')
true
>> /^(https?|ftp):\/\//i.test('telnet://stackoverflow.com/')
false
like image 136
falsetru Avatar answered Nov 10 '22 22:11

falsetru


You can also get this error if you're simply attempting to concatenate a variable that utilizes a regular expression and forget a quotation mark somewhere.

like image 44
Jan Kyu Peblik Avatar answered Nov 10 '22 20:11

Jan Kyu Peblik