寻找加号登录正则表达式 [英] Finding Plus Sign in Regular Expression

查看:120
本文介绍了寻找加号登录正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var string = 'abcd+1';
var pattern = 'd+1'
var reg = new RegExp(pattern,'');
alert(string.search(reg));

我昨晚发现,如果你试图在一串文字中找到加号Javascript正则表达式,它失败了。它不会找到该模式,即使它存在于该字符串中。这必须是因为一个特殊的角色。在一段文字中找到加号的最佳方法是什么?此外,这会失败的其他角色是什么?

I found out last night that if you try and find a plus sign in a string of text with a Javascript regular expression, it fails. It will not find that pattern, even though it exists in that string. This has to be because of a special character. What's the best way to find a plus sign in a piece of text? Also, what other characters will this fail on?

推荐答案

Plus是正则表达式中的一个特殊字符,所以要将字符表示为数据,必须通过在 \

Plus is a special character in regular expressions, so to express the character as data you must escape it by prefixing it with \.

var reg = /d\+1/;

这篇关于寻找加号登录正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆