javascript - Brackets in Regular Expression - Stack Overflow

I'd like to pare 2 strings with each other, but I got a little problem with the Brackets.The Stri

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745550430a4632552.html

相关推荐

  • javascript - Brackets in Regular Expression - Stack Overflow

    I'd like to pare 2 strings with each other, but I got a little problem with the Brackets.The Stri

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信