I'm not the best at regular expressions and need some help.
I have these kind of strings: data-some-thing="5 10 red". Word 'data-some' is constant and 'thing' changes. 'thing' also may contain dashes. The values in double quotes contain only alphanumeric symbols or spaces.
Is it possible to get 'thing' and values in double quotes using only regex? If yes then what expression should I use? I tried using lookarounds but didn't have much success.
You could use:
var result = data.match(/data-some-(.*?)="(.*?)"/);
The result array will have three elements:
Demo:
var data = 'data-some-thing="5 10 red"'; var result = data.match(/data-some-(.*?)="(.*?)"/); document.write(result[1] + '<br>' + result[2]);
Disclaimer:
Please note that if you are doing this in the context of larger HTML parsing (it is not mentioned in the question), you should not use regular expressions. Instead you should load the HTML string into a DOM, and use DOM methods to find the attribute name and value pairs you are interested in.
For node.js you can use the npm modules jsdom and htmlparser to do this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.