简体   繁体   中英

URL rewrite using JQUERY and REGULAR EXPRESSIONS

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:"

$("a[href^='javascript:']")

and change it using regular expression..

But after thousands test I can't solve it..

Any idea? thanks

(<a href=")[^']+'([^']+)','([^']+).*

Try this.Replace by $1$2" title="$3"> .See demo.

http://regex101.com/r/lZ5mN8/14

var re = /(<a href=")[^']+'([^']+)','([^']+).*/gm;
var str = '<a href="javascript: OpenImage(\'/dir1/dir2/Image321.jpg\',\' Image description \');void(0);">';
var subst = '$1$2" title="$3">';

var result = str.replace(re, subst);

try this :

var re = /(^<a\s*href=")javascript:\s*OpenImage\('(.*)'\s*,\s*'(.*)'([^<]+)">/gmi;
var str = '<a href="javascript: OpenImage(\'/dir1/dir2/Image321.jpg\',\' Image description \');void(0);">\n\n<a href="javascript: OpenImage(\'/dir1/dir2/Image321.jpg\',\' Image description \');void(0);"> djhjdh</a>\n\n<a href="javascript: OpenImage(\'/dir1/dir2/Image321.jpg\',\' Image description \');void(0);"><img src="asdf"></a>';
var subst = '\1\2" title="\3">';

var result = str.replace(re, subst);

live demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM