English 中文(简体)
我如何利用贾瓦古典经常表述在字边界之间找到一个用户可提供的说明?
原标题:How can I find a user-supplied string between word boundaries using JavaScript regular expressions?

我有一套案文,用 Java本进行搜索。 我请用户具体说明任意扼杀,然后,我想寻找这种扼杀,条件是它被当作“直言”处理,即二字边界。

我只想说一句话。

var userString = "something blah";
// => "blah another thing blah"
"blah something blah blah".replace(new RegExp("\b" + userString + "\b"), "another thing"); 

// no match, good
"blahsomething blah blah".replace(new RegExp("\b" + userString + "\b"), "another thing");

userString = "something\blah";

// want to match, but doesn t
"blah something\blah blah".replace(new RegExp("\b" + userString + "\b"), "another thing");

如您所看到,它因特殊性质而解体——我需要一种方式来告诉书记官处,以逃避用户的投入,或将部分表述作为字面。 是否可在贾瓦特文中这样做?

最佳回答
userString.replace(/([[]*+.?{}()\^$])/g, "\$1")
问题回答

你们需要正确逃脱后lash。 为了在定期表达中打上一只反弹,你需要一度躲避,以接受常规表达中的实际反弹。 当你将定期表达作为扼杀物时,你需要再次从这两颗反弹中提取每一枚,结果形成<代码><>>>>>>。 因此,你可以简单地逃避您的<密码>用户标准中的鞭lash:

userString = userString.replace(/\/g,  \\ );

请注意,您应使用户的投入变得灵丹妙,以便人们不会经常听到一些令人厌恶的言辞,从而放弃应用。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签