简体   繁体   English

Javascript从文本中提取与正则表达式匹配的字符串

[英]Javascript extract strings that match regex from text

I'm trying to get a set of strings from a paragraph that match the format of 4chan's quotes: >>1111111 where it starts with >> followed by 7 digits.我试图从与 4chan 引号格式匹配的段落中获取一组字符串: >>1111111>>开头,后跟 7 位数字。

>>1111000
>>1111001
Yes, I agree with those sentiments. 

Both >>1111000 and >>1111001 would be extracted from the text above which I would then split into the digits after. >>1111000>>1111001都将从上面的文本中提取出来,然后我将其拆分为后面的数字。

您可以使用此正则表达式

/[>]{2}[0-1]{7}/

You can use the following which will match lines starting with 2 > characters followed by 7 digits:您可以使用以下内容匹配以 2 >字符开头后跟 7 个数字的行:

 const regex =/^[>]{2}[\\d]{7}$/gm; const text = `>>1234567 >>6548789 foo barr`; const matches = text.match(regex); console.log(matches);

There appears to be some answers, but since it's a topic I would like to understand better here are my two cents.似乎有一些答案,但由于这是一个主题,我想更好地理解这里是我的两分钱。 In the past this answer has helped me a lot and online regex sites are also great, such as this one过去, 这个答案对我有很大帮助,在线正则表达式网站也很棒,例如这个

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Parse Test</title> </head> <body> <div > <text id="ToParse"> >>1111000 <br> >>1111001 <br> Yes, I agree with those sentiments. </text> </div> <script> try { var body = document.getElementById('ToParse').innerHTML; console.log(body); } catch (err) { console.log('empty let body,' + " " + err); } function parseBody () { // from HTML // function parseBody (body) { // const regex = /(&gt;&gt;)([0-9]*)\\w+/gm; // from JS const regex = /(>>)([0-9]*)\\w+/gm; const body = ` >>1111000 <br> >>1111001 <br> Yes, I agree with those sentiments.`; let m; while ((m = regex.exec(body)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } }; parseBody(body); // </script> </body> </html>

Like @spyshiv said, you can match the string like so:就像@spyshiv说的,你可以像这样匹配字符串:

var string = '>>1111000';
var matches = string.match(/[>]{2}[0-1]{7}/);
console.log(matches);

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

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