2

Working with vim regexes for folding html, trying to ignore html tags that start and end on the same line.

So far, I have

if line =~# '<\(\w\+\).*<\/\1>'
  return '='
endif

Which works fine for tags like <a></a>, but when dealing with custom elements, I run into issues since there is a hyphen in the tag name.

Like for example, this element

<paper-input label="Input label"></paper-input>

What needs to change in the regex to also catch the hyphen?

2
  • What exactly you are trying to match here? This doesn't sound clear to me. Commented Sep 7, 2016 at 22:44
  • Apologies, maybe a gist would help. gist.github.com/mhartington/96c226aba980513489a9a6fa1d085ecf The regex should match the tags that starts and ends on the sample line Commented Sep 7, 2016 at 22:47

1 Answer 1

2

The correct regex (updated because of this link) is:

<\([^ >]\+\)[ >].*<\/\1>

or

<\([^ >]\+\)\>.*<\/\1>

This is important [^ >]. This will match any character until whitespace or > i.e. it will match both a and paper_input

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

3 Comments

Wonderful, thanks! Interesting, thanks for the info!
@mhartington To be honest I have never used backreference in search pattern. It seems to work but I'm not sure if this is the correct way.
@mhartington I was analyzing it further and asked a question regarding backreference: here. This version: <([^ >]+)[ >].*<\/\1> will be safer.

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.