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

javascript - Brackets in Regular Expression - Stack Overflow

programmeradmin1浏览0评论

I'd like to pare 2 strings with each other, but I got a little problem with the Brackets. The String I want to seek looks like this:

CAPPL:LOCAL.L_hk[1].vorlauftemp_soll

Quoting those to bracket is seemingly useless.

I tried it with this code

var regex = new RegExp("CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll","gi");
var value = "CAPPL:LOCAL.L_hk[1].vorlauftemp_soll";
regex.test(value);

Somebody who can help me??

I'd like to pare 2 strings with each other, but I got a little problem with the Brackets. The String I want to seek looks like this:

CAPPL:LOCAL.L_hk[1].vorlauftemp_soll

Quoting those to bracket is seemingly useless.

I tried it with this code

var regex = new RegExp("CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll","gi");
var value = "CAPPL:LOCAL.L_hk[1].vorlauftemp_soll";
regex.test(value);

Somebody who can help me??

Share Improve this question edited May 16, 2011 at 9:44 Andreas asked May 16, 2011 at 9:35 AndreasAndreas 1937 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

It is useless because you're using string. You need to escape the backslashes as well:

var regex = new RegExp("CAPPL:LOCAL.L_hk\\[1\\].vorlauftemp_soll","gi");

Or use a regex literal:

var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi

Unknown escape characters are ignored in JavaScript, so "\[" results in the same string as "[".

In value, you have (1) instead of [1]. So if you expect the regular expression to match and it doesn't, it because of that.

Another problem is that you're using "" in your expression. In order to write regular expression in JavaScript, use /.../g instead of "...".

You may also want to escape the dot in your expression. . means "any character that is not a line break". You, on the other hand, wants the dot to be matched literally: \..

You are generating a regular expression (in which [ is a special character that can be escaped with \) using a string (in which \ is a special character).

var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi;
发布评论

评论列表(0)

  1. 暂无评论