Regular Expression Help

R

Rick Brandt

I am trying to take a string of HTML content and replace some img elements.
I have managed to come up with a Regular Expression that allows me to
replace ALL img elements, but I need to limit the replacement to those img
elements that have a particular src attribute.

I have been Googling and experimenting all morning, but for the life of me I
can't find the key to this. All attempts either match no img elements, all
img elements, or they match all content from the beginning of the first img
element all the way to the close of the final img element.

I guess the basic requirement is a RegExp that matches a string that begins
with a specified string, contains within a specified string, and then ends
with the first occurrence of a specified string.

Up to now I have been using relatively simple RegExps for form validation
and such, but this one has me stumped.

TIA
 
E

Evertjan.

Rick Brandt wrote on 21 dec 2006 in comp.lang.javascript:
I am trying to take a string of HTML content and replace some img
elements. I have managed to come up with a Regular Expression that
allows me to replace ALL img elements, but I need to limit the
replacement to those img elements that have a particular src
attribute.

Show your code [only the relevant part, please]

I have been Googling and experimenting all morning, but for the life
of me I can't find the key to this. All attempts either match no img
elements, all img elements, or they match all content from the
beginning of the first img element all the way to the close of the
final img element.

Why use a string and not the DOM, something like [not tested]:

for (im in document.images)
if (/blah\d/.test(document.images[im].src))
document.images[im].src = 'default.jpg';
 
R

Rick Brandt

Evertjan. said:
Rick Brandt wrote on 21 dec 2006 in comp.lang.javascript:
I am trying to take a string of HTML content and replace some img
elements. I have managed to come up with a Regular Expression that
allows me to replace ALL img elements, but I need to limit the
replacement to those img elements that have a particular src
attribute.

Show your code [only the relevant part, please]

Thanks for the response, but I got it figured out. The problem was
complicated by the fact that the string I was matching on contained [ and ]
characters. Also by the fact that the string being searched was the result
of an XSL transformation and what comes out is not exactly the same as I had
defined in the style sheet (Firefox disposed of an unnecessary closing tag
while IE did not).

The reason I am not using the DOM is that I am not trying to modify the DOM.
I am just using the HTML string to pass to another process.
 
D

Dr J R Stockton

In comp.lang.javascript message
Thu said:
I am trying to take a string of HTML content and replace some img elements.
I have managed to come up with a Regular Expression that allows me to
replace ALL img elements, but I need to limit the replacement to those img
elements that have a particular src attribute.

St = 'a<img src="123">b<img src="124">c<img src="125">d<img src="126">e'

M = St.match(/<img\s+src="125"[^>]*>/g)

works for me in js-quick.htm.

It does assume that src is the first attribute and that quotemarks are
as shown.

It could be fooled by perverse HTML such as

aaa<img fred='xxxsrc="125"yyy' src="124">zzz which does not contain a
src 125 attribute.

M = St.match(/<img[^>]+src=(['"])125\1[^>]*>/g) may be better.

Note : to search for something NOT including "src" one can firstly
replace all "src" by some obscure character such as \u00BF or \u2302 and
secondly use [^\u00BF] in the search.

It's a good idea to read the newsgroup and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top