I am modifying an existing HTML doc. I'm doing things like adding a table of contents etc.
I have a heading with this ID: id="transcending intellectual limitations"
(for real!)
I want to be able to find the whole ID, and then replace the spaces with hyphens.
It would be simple if I had just the IDs but I don't want to remove all the spaces in the whole document.
I'm reasonably new to regex, I'm using Sublime's find and replace to do this.
You can use
(?:\bid="|(?!^)\G)[^\s"]*\K\s+
And replace with anything you need to replace spaces with.
The (?:\bid="|(?!^)\G)
pattern sets the initial boundary: either id="
or the end of the last successful match. This pattern presents an alternation list with two alternatives. \b
matches a word boundary so that id="
is matched as a whole word. The \G
operator matches at the start of the string and after ech successful match. To exclude the start position, a negative (?!^)
lookahead is added (not followed with a string start position).
See more about \G
in "Where You Left Off: The \G
Assertion".
The [^\s"]*
matches zero or more characters other than whitespace and a quote.
The \K
operator makes the regex engine omit all the text matched so far from the match buffer.
The \s+
finally matches one or more whitespaces that will be replaced.
Regex101 Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With