Skip to content Skip to sidebar Skip to footer

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:

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"