0

I have match a group, but I don't know how to get the required extract.

I have this string:

<myTagName >
<mySecondTagName >

And I have this regular expression:

^(\s|\d).*?\<+([a-zA-Z])([0-9]|_|-|[a-zA-Z])*+(\s|\d)+(>)

And then I want to get the tag name only if the criteria matches. How should I specify this capturing group?

I mean I say Javascript to return me the tag name only if before it comes some certain characters, and is also followed by some the other set. The rules are specified already in the given regexp, but they are unconditional and without any look-around.

1
  • This seems to be your regex in a boiled down state: ^[\s\d].*?<[a-zA-Z][\w-]*\s*> But you should give better examples.
    – user557597
    Feb 24, 2014 at 22:06

3 Answers 3

0

Actually that's what you need

<([a-zA-Z][\w-]*)[\s]*>

The string inside the first capture group is your tagname.


Notes to your regex:

A leading ^[\s\d]* means only spaces or digits are allowed before the actual tag... why digits?

Some of the original constructs make not much sense for the matching behaviour you require:

(\s|\d)+ // This means capture at least one space or digit and put in a group

.*?\<    // Non greedy any character until < is found => use [^<]* instead, better performing

<+       // Means at least one <,a and is here just a workaround for the not neccessary non greedy match all

([a-zA-Z])([0-9]|_|-|[a-zA-Z])*+  // Here you wanted to say a string that starts with a character but actually you have two capture groups here and the *+ makes no sense for me (at least one word?) 

(\s|\d)+   // At least one space or digit? why digit? there really MUST be a space? You really want to capture it?

(>)       // You want to capture the last > ? for what?
0
0

You won't get a capture out unless the whole thing matches anyway.

However, you appear to not be capturing the entire tagName in one item anyway as your $1 is the space or digit at the start, your $2 is the first letter of the tag name, your $3 is the next character repeated (which will end up being the last character of the tag name as the * is not inside the ()s), your $4 is the space or digit at the end of the tag (same issue with + outside the ()s) and your $5 is the final >

You are probably trying to do something more like this:

^(\s|\d).*?<([A-Za-z][A-Za-z0-9_-]*+)((?:\s|\d)+)> where $1 is the first space or digit, $2 is the entire tagName and $3 is the final run of spaces or digits. (Note the use of the non-capturing group (?: ) inside $3.)

0

First, you don't want to parse xml/html with regex. Just don't (RegEx match open tags except XHTML self-contained tags)

Secondly, here's a few thoughts about your regex:

  • .*? This does not really make sense, you used two quantifiers for the dot.
  • \<+ Do you really want to match things like <<<tag>?
  • ([0-9]|_|-|[a-zA-Z]) This can be simplified to ([-0-9_a-zA-Z])
  • *+ Again the double quantifier
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.