admin管理员组文章数量:1323179
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
. What I want is all three matching i.e:
+2143334, +234343443, +76645
How can it be done?
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
. What I want is all three matching i.e:
+2143334, +234343443, +76645
How can it be done?
Share Improve this question edited Mar 8, 2018 at 12:38 Biffen 6,3516 gold badges31 silver badges37 bronze badges asked Mar 8, 2018 at 12:34 DomnickDomnick 5191 gold badge9 silver badges25 bronze badges 4- 1 You’ll probably want to use a regex. How familiar are you with them? – Ry- ♦ Commented Mar 8, 2018 at 12:36
-
Do you only want to extract
+
followed with diigts afterwant
? Or any non-whitespace chunks? – Wiktor Stribiżew Commented Mar 8, 2018 at 12:38 - @Ryan not much familiar with regex – Domnick Commented Mar 8, 2018 at 12:40
- @WiktorStribiżew I want to extract + followed with digits after want. – Domnick Commented Mar 8, 2018 at 12:41
5 Answers
Reset to default 6You may use the following regex to capture the +
followed with 1+ digits after want
:
/want\s*(\+\d+)/g
See the regex demo.
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.
In Chrome, you may even use str.match(/(?<=want\s*)\+\d+/g)
, but not all browsers already support the ECMAScript 2018 cool regex features.
JS demo:
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 )([^ ]*)(?= !)
:
(?<=want )
makes sure want[space]
is behind your expression
([^ ]*)
matches anything but a space
(?= !)
makes sure [space]!
is after your expression
The g
is added to make the RegEx global.
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. 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\!
if for text after your match.
Best regards ;)
You can use this one
str.match(/want\s[a-zA-Z0-9+!@#$%^&*()_+={}|\\]+\s!/g).map((e)=>e.split(' ')[1])
本文标签: regexExtract text between two words from entire string JavaScriptStack Overflow
版权声明:本文标题:regex - Extract text between two words from entire string JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742131076a2422161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论