Getting The First Word In A Tag With Regex
I'm trying to make a regex to get the first word in a < ... > tag. I already have one to get all the words in a tag. I'm using this for it: /<(.*?)>/ My question is, c
Solution 1:
Here's a working solution: /<([^>\s]+)[^>]*>/
Solution 2:
This will do it...
/<(.*?)\s/
Solution 3:
In simple case /<(\S+)/
might be what you are looking for, or you might be more formal and actually list only characters allowed in tag names.
Solution 4:
var tag = "< hello world>";
var regex = /<\s*(\S*)\b/;
if (match_arr = tag.match(regex)) {
alert("yes:" + match_arr[1]);
}
is there any way to get every word after the first word in a tag?
var tag = "< hello world goodbye >";
var regex = /<(.*?)>/;
if (match_arr = tag.match(regex)) {
var str = match_arr[1].trim();
var words = str.split(" ");
console.log("-->" + words.slice(1).join(" ") + "<--");
}
--output:--
-->world goodbye<--
Post a Comment for "Getting The First Word In A Tag With Regex"