简体   繁体   English

javascript正则表达式示例-提取字符直到出现?

[英]javascript regex examples - extract until character appears?

I have a text like this. 我有这样的文字。

Mr John Smith , Mr James Smith                  +(21)-(21)-12345678, 12345678, 12345678                     +(21)-12345678, 12345678, 12345678        SomeTextHereAlso        +(21)-(22)-12345678                       www.somewebaddress.co.uk                            Some Title, Some Place , Some Town,Some Suburb,  City - 100000

I want to extract each of these strings using regex in javascript. 我想使用javascript中的正则表达式提取这些字符串中的每个字符串。 I found some examples and this morning they worked. 我找到了一些例子,今天早上他们工作了。 Now I don't know why they don't work any more. 现在我不知道为什么他们不再工作了。

To extract 提取

Mr John Smith , Mr James Smith

I used this. 我用这个

/\S(.*)\+/  and /\S(.*?)\+/

This didn't work. 这没用。 I can't figure out why. 我不知道为什么。

To extract this 提取这个

+(21)-(21)-12345678, 12345678, 12345678                     +(21)-12345678, 12345678, 12345678        SomeTextHereAlso        +(21)-(22)-12345678     

I used this. 我用这个

/\+(.*)(?=www.)/

This did work. 这确实有效。

And for url I used 对于我使用的网址

/www(.*?)(?=\s\s)/

And this works too. 这也适用。

The only problem is the first example that should extract all the characters until the first + but it extracts all the characters until the last +. 唯一的问题是第一个示例,该示例应该提取所有字符,直到第一个+,但它提取所有字符,直到最后一个+。

I checked on http://gskinner.com/RegExr/?2tr5t and the examples I showed here work. 我在http://gskinner.com/RegExr/?2tr5t上进行了检查,在此显示的示例可以正常工作。 Are there any more similar examples that could help me since I looked into the code and didn't find any error. 自从我研究了代码之后,没有发现任何错误,是否还有更多类似的示例对我有帮助。

If regex is fine then how can I use IndexOf() method for this example to extract what I want? 如果正则表达式没问题,那么如何在本示例中使用IndexOf()方法提取所需的内容?

I used This solution help me solve my problem. 我用过的解决方案可以帮助我解决问题。 So my project will be combination of RegEx and IndexOf() method. 因此,我的项目将是RegEx和IndexOf()方法的组合。

I don't know why RegEx failed on this matter. 我不知道为什么RegEx在这件事上失败了。 Thank you all. 谢谢你们。

Well, for the first example, you could just forbid the inner loop from matching "+" characters, using a negated character class. 好吧,对于第一个示例,您可以使用否定的字符类来禁止内部循环匹配“ +”字符。

/\S([^+]*)\+/

As for extracting the matches, a common pattern is doing something like 至于提取匹配项,常见的模式是

var myPattern = /someregex/g;
var match;
while( (match=myPattern.exec(theString)) !== null ){
    console.log('the total match', match[0]);
    console.log('the first grouped parenthesis', match[1]);
    //and so on
}

For example, running 例如,跑步

var regex = /a(\d+)/g;
var str = ' a100 b200 a300';
var match;
while( (match=regex.exec(str)) !== null ){
    console.log(match)
}

prints 版画

["a100", "100"]
["a300", "300"]

If you have any more doubts, I highly recomend ythe documentation over on MDN 如果您还有其他疑问,我强烈建议您使用MDN上的文档

您可以尝试[^+]+或更具体地说^[^+]+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM