Kanishk Dudeja
Kanishk Dudeja

Reputation: 1221

Regular Expression Sublime Text Search and Replace

I have a bunch of html files.

I am trying to find all anchor links whose href attribute does not end with slash

For example :-

<a href="/hello">Helllo</a>

This should match

<a href="/hello/">Helllo</a>

This should not match

How do i go on about making the regular expression.

Upvotes: 1

Views: 616

Answers (4)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

In order to make sure you only collect href attribute values from <a> elements, and to make sure you only match the link itself, you can use the following regex:

<a\s[^<>]*href="\K[^"]*?(?<=[^\/])(?=")

Or

<a\s[^<>]*href="\K[^"]*?(?=(?<=[^\/])")

enter image description here

Upvotes: 2

Pedro Lobito
Pedro Lobito

Reputation: 98871

You can try:

Find: href="(.*?[^/])"

Replace with: href="$1/"

Upvotes: 2

James Baxter
James Baxter

Reputation: 1246

Building on hjpotter92's answer...

Find: href="(\S*?[^/])"

Replace: href="$1/"

Upvotes: 3

hjpotter92
hjpotter92

Reputation: 80629

The following pattern would work:

href="\S*?[^/]"

Upvotes: 2

Related Questions