Reputation: 49411
I have an HTML file opened in Atom, with a lot of links.
I want to replace all spaces by underscores. But only spaces that are inside a href=""
!
I've managed to write a regex search that seems to find the correct occurrences:
href="(.)+ (.)+"
The problem is now to write a replace string to replace the spaces.
Upvotes: 0
Views: 1320
Reputation: 188
Simple use something like :
href.replace(/\s/g, "_");
The \s is an alias for all blank caracters (space, carriage return, line feed, tabs)
Edit : Sorry I haven't answered before, but I can only give you an answer that will replace only the last occurrence of the space in the href, must click several times on the replace all button to replace all the spaces with underscores. I've just installed atom to discover it's regex.
Search for :
href=\"([^"]*)(\s)(.*)\"
Replace with :
href="$1_$3"
Tested with the following code :
<html>
<head>
<script type="text/javascript" src="somewhere/in another/dir.js"> </script>
</head>
<body>
<p>
<a href="first link.pdf">1st link</a>
<a href="my/second link.html">2nd link</a>
<a href=" a third/one just to comfirm/that it works well link.html">3rd link</a>
</p>
</body>
</html>
Apply it a first time will result with the following code :
<html>
<head>
<script type="text/javascript" src="somewhere/in another/dir.js"> </script>
</head>
<body>
<p>
<a href="first_link.pdf">1st link</a>
<a href="my/second _link.html">2nd link</a>
<a href=" a third/one just to comfirm/that it works well_link.html">3rd link</a>
</p>
</body>
</html>
But you can do all in one if you accept to change you editor for this replacement. Can be easily replace in one execution with sed or awk.
Upvotes: 2