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

javascript - Error when using String.search("sinh(2"): "Invalid regular expression" - Stack

programmeradmin1浏览0评论

I have problem as below:

var test = $("#k_w").val().search("sinh("+parseFloat(sinh_array[i]));

The debugger shows an error: Uncaught SyntaxError: Invalid regular expression: /sinh(2/: Unterminated group.

sinh_array[i] are numbers.

What's wrong?

I have problem as below:

var test = $("#k_w").val().search("sinh("+parseFloat(sinh_array[i]));

The debugger shows an error: Uncaught SyntaxError: Invalid regular expression: /sinh(2/: Unterminated group.

sinh_array[i] are numbers.

What's wrong?

Share Improve this question edited Mar 18, 2012 at 21:14 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Mar 18, 2012 at 13:09 Marcin KostrzewaMarcin Kostrzewa 5954 gold badges11 silver badges24 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

The String.search method converts the first argument to a Regular expression.

You are looking for the String.indexOf method, which search for a string, without a conversion to a RegExp.

var test = $("#k_w").val().indexOf("sinh("+parseFloat(sinh_array[i]));
//                         ^^^^^^^ indexOf

You have an opening parenthesis in your regex, but no closing parenthesis.

I think what you really want is this:

var test = $("#k_w").val().search("sinh\\("+parseFloat(sinh_array[i]) + "\\)");

I suspect you want to match the actual parens, and not create a group.

You have to escape parentheses in regexps; otherwise they begin a match group and thus have to be closed again.

var test = $("#k_w").val().search("sinh\\("+parseFloat(sinh_array[i]));
发布评论

评论列表(0)

  1. 暂无评论