0

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, can I get the first word in a tag using a regex?

4

4 回答 4

2

Here's a working solution: /<([^>\s]+)[^>]*>/

于 2013-04-30T01:22:08.153 回答
0

This will do it...

/<(.*?)\s/
于 2013-04-30T00:52:08.417 回答
0

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.

于 2013-04-30T00:55:04.360 回答
0
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<--
于 2013-04-30T01:02:46.000 回答