Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to get span tag [duplicate]

Tags:

regex

May I know what is the regular expression rule for matching html tag

<span id="id1" class="class1" attribute="attribute1">Test</span>

that matches the tag SPAN regardless the property in it.

My existing rule is this, but not working.

/(<span [^>]*>)>/s
like image 689
davidlee Avatar asked Jun 04 '14 04:06

davidlee


People also ask

What is span in regular expression?

span() method returns a tuple containing starting and ending index of the matched string. If group did not contribute to the match it returns(-1,-1). Parameters: group (optional) By default this is 0. Return: A tuple containing starting and ending index of the matched string.

What can I use instead of span?

Div tags are basically <span> tags for block elements instead of inline elements. So if I want for example a text to have a specific size I should use a div where my text is inside? Is this really a good way? Yep.

What is CSS regex?

You can use regular expressions (regex) and cascading style sheet (CSS) selectors as operators wherever trigger filters are used. When a regular expression or CSS selector is set as the operator for a trigger, you can specify that the trigger matches the rule.

What is the span tag?

The <span> HTML element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang .


3 Answers

David, the reason (<span [^>]*>)> is that you have a small typo.

You see, that expression tries to match two closing >: look closely at the end >)>. For instance, it would match <span hey there>> but not <span hey there>

To match the opening span, make sure you only have one >.

With all the disclaimers about using regex to match html, this regex will do:

<span[^>]*>

If you sometimes expect SPAN, make sure to make it case-insensitive.

Only if you have time: an additional flourish

In a comment, @DavidEhrmann points out that the regex above will match <spanner>. If you want to make him happy and ensure that if the span is more than just <span> it always contains a space after span, you can use:

<span(?: [^>]*)?>

However, in my view, that is an unnecessary flourish. When we parse html with regex, we always know that we are using a crude tool, and we rely on the input to be fairly well-formed. For instance, with the revised regex above, there are still a million ways that we can match improper html, for instance: <span classification>

What to do? Nothing. Know your tools, know what they can do, know the risks, and decide when the situation warrants regex and when it warrants a DOM parser.

like image 91
zx81 Avatar answered Oct 06 '22 01:10

zx81


Take a look at here Regex for Html Tags

Try This

<span[^>]*>[\s\S]+<\/span>

Regex Demo


O/ P:

enter image description here

like image 37
Vignesh Kumar A Avatar answered Oct 06 '22 00:10

Vignesh Kumar A


/<span[^>]*>[^>]*<\/span>/
like image 42
Fabricator Avatar answered Oct 05 '22 23:10

Fabricator