English 中文(简体)
校正中的 Regex 匹配
原标题:Regex match in javascript

我想用regex来比对JavaScript的下个词。

String =  abc AND def AND igk AND lmn 

Terms to match: the word before and after first AND.

例如,在上述字符串中,匹配部分将是:abc和 def

我想在JavaScript做,所以我会打电话给

string.match(/regex to use/)

并把它分配到一个var。

请提出任何建议。

编辑:

字符串的形式可以如下:

    string =  AND abc ;
    string =  abc AND ;
    string =  abc def AND igk lmn ;
    string =  abc def AND ;
    string =  AND igk lmn ;

感谢你在这方面的帮助。

最佳回答

伊戈尔的版本稍有改进:

   var m = str.match (/(w+)s+ANDs+(w+)/);
   // word before AND in m[1], word after AND in m[2] 

自动地提取这两个词。

问题回答

您可以尝试此正则 :

/(w+s+ANDs+w+)/

<强度 > EDIT (在阅读了上次更新之后):如果左和右术语是可选的,则使用以下正则替换:

/(?: (w+) s+)? AND (?: s+ (w+))?/x




相关问题
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.

热门标签