匹配多行正则表达式Javascript [英] Match Multiple Line Regex Javascript
问题描述
在整个晚上的大部分时间里,我都把头撞在墙上,所以我期待Stackoverflow众神对此提供帮助.我有一个试图解析为数组的文本字符串.请参见下面的示例:
I have beat my head against the wall for the better part of the night so I am looking to the stackoverflow Gods to help with this. I have a text string that I am attempting to parse into an array. See the example below:
Name: John Doe
Address: 123 W Main Street
City: Denver
Name: Julie Smith
Address: 1313 Mockingbird Lane
City: Burbank
我想要得到的是一个看起来像这样的数组:
What I would like to get is an array that looks like this:
[Name: John Doe Address: 123 W Main Street City: Denver, Name: Julie Smith Address 1313 Mockingbird Lane City: Burbank]
我从以下正则表达式开始,它在正则表达式中工作正常:
I started with the following regex and it works fine in regexpal:
(Name.+)(\n.*){2}
但是,当我尝试在程序甚至jsfiddle中使用它时,会返回null:
However, when I try to use that in my program or even jsfiddle, I get a null return:
有什么想法吗?
推荐答案
我不是JS专家,但经过一些工作后,我可以提供以下脚本:
I am not a JS expert but after a bit of work around i can offer this script:
var rawdata='\
\
\
Name: John Doe\
Address: 123 W Main Street\
City: Denver\
\
\
Name: Julie Smith\
Address: 1313 Mockingbird Lane\
City: Burbank';
var looppattern = /Name: /g;
var loopnum = rawdata.match(looppattern);
var pattern = /\s{2}([\w\:\s]+)/;
var nummessages = rawdata.replace(new RegExp("Name:", 'g'),"{Name:").split("{");
alert(nummessages[1]);
alert(nummessages[2]);
似乎可以在JSFiddle中使用.在这里,我认为'{'不在您可以更改此定界符的字符串之间.
which seems to work in JSFiddle. Here i supposed '{' does not lie in between the string you can change this delimiter.
这篇关于匹配多行正则表达式Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!