1

I'm using regex in javascript to check if a string contains a tag called 'tag-name'. See below:

    someText
    <other>more text<other>
</tag-name>

I want to extract "tag-name", only if it occurs following the first instance of '<' in the string. So in the example above, my regex should not find a match as there is a '<' character at the 'other' tag.

In this example below, it should return 'tag-name' as it occurs after the first occurance of '<'.

someText
</tag-name>
<other></other>

Can anyone suggest how I can get this to work, so far I have used this expression:

<*?tag-name

but, it is extracting the tag-name string regardless of where it occurs.

Thanks for your help.

0

1 Answer 1

1

You can use this regex:

/^[^<]*<[^<]*tag-name/

The logic is to match the start of the string, zero or more non-< characters, <, zero or more non-< characters, then your tag name.

Demo — Be sure to see the Unit Tests (link on the lower left) to demonstrate both of your cases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.