开发者

Brackets in Regular Expression

I'd like to compare 2 strings with each other, but I got a little problem with the B开发者_运维问答rackets. 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??


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜