[英]Extract text between two words from entire string JavaScript
I have a string where I want to extract all the words that is between two text, such that: 我有一个字符串,我想在其中提取两个文本之间的所有单词,例如:
var str="This is the number I want +2143334 !, again this is the next number I want +234343443 !, last number I want +76645 !, fininshed";
var ext = str.split('want').pop().split('!,').shift();
alert(ext);
But this gives only +2143334
. 但这只给出
+2143334
。 What I want is all three matching ie: 我想要的是所有三个匹配项,即:
+2143334, +234343443, +76645
How can it be done? 如何做呢?
You may use the following regex to capture the +
followed with 1+ digits after want
: 您可以使用下面的正则表达式来捕捉
+
接着用后1+位数want
:
/want\s*(\+\d+)/g
See the regex demo . 参见regex演示 。
Here, want
matches a literal substring, then \\s*
matches 0+ whitespace chars and the (\\+\\d+)
captures into Group 1 a plus sign and then 1+ digits. 在这里,
want
匹配一个文字子字符串,然后\\s*
匹配0+个空格字符,而(\\+\\d+)
捕获到组1中的一个加号和一个1+位数。
In Chrome, you may even use str.match(/(?<=want\\s*)\\+\\d+/g)
, but not all browsers already support the ECMAScript 2018 cool regex features. 在Chrome中,您甚至可以使用
str.match(/(?<=want\\s*)\\+\\d+/g)
,但并非所有浏览器都已支持ECMAScript 2018酷正则表达式功能。
JS demo: JS演示:
var str="This is the number I want +2143334 !, again this is the next number I want +234343443 !, last number I want +76645 !, fininshed"; var m,results = []; var rx = /want\\s*(\\+\\d+)/g; while(m=rx.exec(str)) { results.push(m[1]); } console.log(results);
You can use the RegExp (?<=want )([^ ]*)(?= !)
: 您可以使用RegExp
(?<=want )([^ ]*)(?= !)
:
(?<=want )
makes sure want[space]
is behind your expression (?<=want )
确保want[space]
位于表达式后面
([^ ]*)
matches anything but a space ([^ ]*)
匹配除空格以外的任何内容
(?= !)
makes sure [space]!
(?= !)
确保[space]!
is after your expression 在你的表情之后
The g
is added to make the RegEx global . 添加
g
以使RegEx 全局化 。
var str = "This is the number I want +2143334 !, again this is the next number I want +234343443 !, last number I want +76645 !, fininshed"; console.log(str.match(/(?<=want )([^ ]*)(?= !)/g));
Actually your code is giving +76645 as result. 实际上,您的代码给出的结果是+76645。 Anyway, the more straigthforward way to do it is as follows:
无论如何,更简单的方法如下:
var str="This is the number I want +2143334 !, again this is the next number I want +234343443 !, last number I want +76645 !, fininshed"; // To extract numbers only var vetStrings = str.match(/\\d+/g); console.log(vetStrings); // To cast the result as numbers var vetNumbers = vetStrings.map(Number); console.log(vetNumbers);
:) :)
My suggestion: 我的建议:
var reg = /(?<=want\s)[a-zA-Z0-9+]+(?=\s\!)/g;
var yourText = 'This is the number I want +2143334 !, again this is the next number I want +234343443 !, last number I want +76645 !, fininshed';
var resultArray = yourText.match(reg);
console.log(resultArray);
Where 哪里
want\\s
想要
(\\s is for space) is text before your match, and (\\ s为空格)是您的匹配项之前的文本,并且
\\s\\!
\\ s \\!
if for text after your match. 如果是比赛后的文字。
Best regards ;) 最好的祝福 ;)
你可以用这个
str.match(/want\s[a-zA-Z0-9+!@#$%^&*()_+={}|\\]+\s!/g).map((e)=>e.split(' ')[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.