How to search and replace under a regex pattern in vscode

Mary Ma

I am trying to replace '-' (hyphen) in the file name to '__' (underscore)in a specific regex pattern.

For example, changing /abc/123-def-xyzz.html to /abc/123_def_xyzz.html.

I tried to use the regex below to match the file name by separating the text with hyphen and the text with extension. It matches /123_def_.

\/((.[^/]*))-+(?=.*\.html)

However, I fail to replace hyphen to underscore in this pattern. Using $1_ will result in /123-def_xyzz.html.

Wiktor Stribiżew

You can use

-(?=[^/\s]*\.html\b)

Replace with _. See the regex demo.

The regex matches

  • - - a hyphen
  • (?=[^/\s]*\.html\b) - that is immediately followed with
    • [^/\s]* - zero or more chars other than whitespace and / chars
    • \.html\b - .html as a whole word (no letter, digit or _ are allowed right after l).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related