Jquery and regular expression to build new href

I browsed all the afternoon trying to find a good solution, but with no luck.

This is the problem:
I have an old html website. I have to change every link built in this way:

<a href="javascript: OpenImage('/dir1/dir2/Image321.jpg',' Image description ');void(0);">

with a standard a href:

 <a href="/dir1/dir2/Image321.jpg" title="Image description">

I think I have have to look for href beginning with “avascript:”


and change it using regular expression…

But after thousands test I can’t solve it…

Any idea?

Wouldn’t it be better to write a script in a scripting language (such as Ruby) which permanently changed the link structure in the files themselves?

This way you could run the script just once and not every time any of your pages is rendered.

Yea the script way would be best, or even just a find/replace throughout all your files. I’m not very good with regular expressions, but in case you can’t edit them that way, and assuming all hrefs use double quotes:

	var $this = $(this)
	var src = $this.attr('src')
	if(src.indexOf('javascript:') === 0){
		var parts = src.split("'")

This splits the strings along single quotes, so for your example the array would be structured like this:

javascript: OpenImage(
 Image description 

Thanks any suggestion to a way to batch change every html file with that kind of substitution?

Good code editors such as Notepad++ or Atom let you perform a regular expression find/replace over multiple files.

Good point.
What kind of feedback do these give you about any potential find and replace operations?
Using a scripting language you can output all manner of information to the terminal (such as which files will be affected, which lines your regex matched, or what the line looks like once the replace has been performed), as well as perform a dry-run. Is the same possible using Notepad++ for example?

What’s your OS?
The reason I ask is that Mac and Linux come with Ruby pre-installed, which would be advantageous if you were to go the scripting route.

With Atom for example, the potential matches are all marked by a line box around the text.
The find results across multiple files are also output to a separate log section, so that you can scan through them to check for any irregularities.

They’ve thought about this process quite well.

That sounds nice.
Maybe Atom is worth looking into.

Atom is a free open source version of Sublime Text, so yes - it definitely is worth checking out.

I just installed it.
I’ll give it a spin this morning.

Hi, I downloaded Atom (it’s GREAT!) but I could not find a way to do a batch search/replace across all my html local pages


It’s probably better to wait for Paul to explain how to do this with Atom.

However, if you fancy installing any version of Ruby, to accomplish this, I can help you with that.

Here’s the latest one-click installer: http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.1.4.exe?direct

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.