Reputation: 740
I am trying to create a regex pattern to find and then replace my IMG src tags in my HTML template.
Essentially, the pattern should find the contents of the src:
<img src="http://www.domain.com/img/building.jpg" />
And then replace it as such:
<img src="http://www.domain.com/image/timthumb.php?src=http://www.domain.com/img/building.jpg&w=800" />
I essentially need to append to the beginning and end of the existing src attribute.
What would be the pattern to find and then the pattern to replace?
Upvotes: 1
Views: 4315
Reputation: 89547
You can try with that:
find="(<img\b(?:[^s>]+|\Bs|s(?!rc\s*=\s*))+src\s*=\s*)[\"']?((http:\/\/)?([^\/]+)\/(?:[^\/\s]+\/)*)([^\"'\s>]+)[\"']?"
replace="$1\"$3$4/image/timthumb.php?src=$2&w=800\""
Upvotes: 0
Reputation: 10003
For example:
your_string.replace(/(<img\ssrc=")(.*?)("\s?\/>)/, "$1http://www.domain.com/image/timthumb.php?src=$2&w=800$3");
Example:
> '<img src="http://www.domain.com/img/building.jpg" />'.replace(/(<img\ssrc=")(.*?)("\s?\/>)/, "$1http://www.domain.com/image/timthumb.php?src=$2&w=800$3")
"<img src="http://www.domain.com/image/timthumb.php?src=http://www.domain.com/img/building.jpg&w=800" />"
Upvotes: 1