I need to extract 3 fields from a snippet of text. I’ve got a regex that extracts 2 of those fields – file and extension. I don’t know how to extract the content text and I don’t have a strategy for matching all characters except ‘not a pattern’.
The pattern is: var regex_file = /<!--<\|(.*)\.(.*)\|>-->/g,
Also, I’m not sure a regex is the best way to do this. I considered the string method split: split(regex_file)
But I don’t think there is a way to keep the delimiter one splits on. It just returns the the content between the delimiter. Also, I don’t think there is a way to loop through split() as I did through exec() below.
What is the best way to extract these 3 fields from text in the structure below. Further below is what I have so far.
Text Structure
<!--<|file.extension|>-->
// 1-10k of content text
<!--<|file.extension|>-->
// 1-10k of content text
<!--<|file.extension|>-->
// 1-10k of content text
First Attempt
/*addNodes
**
**
**
*/
function addNodes(text) {
var regex_file = /<!--<\|(.*)\.(.*)\|>-->/g,
arr_file;
while ((arr_file = regex_file.exec(text)) !== null) {
arr_file[1] // holds file
arr_file[2] // holds extension
arr_file[3] // need content here
}
}
Additional Criteria:
IE10+, FF10+, Safari5+, Chrome20+…Major Modern Browsers.
You were pretty close. The delimiter can be kept with regex:
output:
I was able to manually test it successfully for ie9, chrome23 on windows and chrome23, safari6, ff6 on on osx10.
I also threw it on browsershots.org and execution was successful for these versions:
firefox: 3.6.27 4.0.1 5.0.1 1.5.0.12 10.0.2 6.0.1 11.0 11.0 12.0 13.0 14.0.1 17.0 7.0.1 15.0 16.0 2.0.0.20 8.0.1 9.0.1 18.0 17.0 19.0 3.6.28 16.0 9.0.1 10.0.2 17.0 6.0.2 7.0.1 8.0 12.0 14.0.1 13.0.1 4.0.1 15.0 5.0.1 3.0.10 19.0 18.0
msie: 10.0
safari: 3.2.3 5.1.7 5.1.7 6.0 5.0
chrome: 7.0.517.44 23.0.1271.101 17.0.963.56 8.0.552.224 17.0.963.56 22.0.1229.26 23.0.1251.2 22.0.1312.45 9.0.597.107 11.0.696.77 10.0.612.1 12.0.742.112 13.0.782.218 14.0.835.202 15.0.874.106 18.0.1025.33 19.0.1041.0 20.0.1132.57 21.0.1180.89 24.0.1312.45 23.0.1271.97 16.0.912.77 2.0.172.31 6.0.472.63
It was not successful for:
chrome 20.0.1132.47 on ubuntu-12.04-lts
or versions of msie < 10