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?

mhartington
  • 6,905
  • 4
  • 40
  • 75
  • What exactly you are trying to match here? This doesn't sound clear to me. – Dave Grabowski Sep 07 '16 at 22:44
  • Apologies, maybe a gist would help. https://gist.github.com/mhartington/96c226aba980513489a9a6fa1d085ecf The regex should match the tags that starts and ends on the sample line – mhartington Sep 07 '16 at 22:47

1 Answers1

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

Community
  • 1
  • 1
Dave Grabowski
  • 1,619
  • 10
  • 19
  • Wonderful, thanks! Interesting, thanks for the info! – mhartington Sep 07 '16 at 23:35
  • @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. – Dave Grabowski Sep 07 '16 at 23:37
  • @mhartington I was analyzing it further and asked a question regarding backreference: [here](http://stackoverflow.com/questions/39380964/vim-sed-regex-backreference-in-search-pattern). This version: `<([^ >]+)[ >].*<\/\1>` will be safer. – Dave Grabowski Sep 08 '16 at 04:59