GoldenAge
GoldenAge

Reputation: 3068

Add a phrase to the end of each occurrence in Visual Studio Code using Regex starts with and doesn't end with and doesn't contain special sign

I want to build an expression which returns all the phrases which start with a given string and not ends with a given string and contain a special character Example:

Starts with href=" doesn't contain . and not ends with /

Matches

href="blah/foo"
href="foo"

Doesn't match

href="blah/foo.css"
href="blah/foo.ico"
href="blah/foo."
href="blah/"
href="blah/foo.css/"

I tried to build something like this (href=\")^((?!.).)|(?!.*\/$)*$but I'm stuck at the moment.

I also want to be able to add a phrase to the end of each occurrence.

Upvotes: 1

Views: 507

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You may use

Find: (href=")([^".]*[^"./])(")
Replace: $1$2.html$3

See the regex demo

Details

  • (href=") - Group 1 ($1): href=" substring
  • ([^".]*[^"./]) - Group 2 ($2): any 0+ chars other than " and . followed with any char but ", . and /
  • (") - Group 3 ($3): a " char.

Test in VS Code:

enter image description here

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37755

You can try this mate

href="[^.\s]+?(?!\.)[^\/.]"$

Explanantion

  • href=" - Will match exactly href=".
  • [^.\s]+? - Will match anything except . and space one or more time (lazy mode).
  • (?!\.) - Negative look ahead. It checks for ..
  • [^\/.]$ - Checks for end of string should not contain / or .

Demo

Upvotes: 0

Related Questions