最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - unterminated regular expression literal js error - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Nov 9, 2013 at 6:24 SamSam 5,26012 gold badges48 silver badges98 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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./')
true
>> /^(https?|ftp):\/\//i.test('telnet://stackoverflow./')
false

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.

发布评论

评论列表(0)

  1. 暂无评论