0

I am not very good with Regular Expressions, some times I can figure them out but...

I need to parse text strings (for a chat room project).

So as you would imagine any pasted URLs need to be converted to click-able hyper links.

I use this RegExp for that, cobbled together from examples I have found on the net. It appears to work quite well :

/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:~;@'#%&.=\]\[\*\$\!\?\/\,]+/g

Now another part of my project has to insert images in other words :

<img src="http://path/to/image" alt="alt" />

So I need the reg exp to ignore those, and I tried this :

/(?!src=")[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:~;@'#%&.=\]\[\*\$\!\?\/\,]+/g

But it doesn't work. Perhaps my expression is faulty or I am going about it the wrong way.

I may just mask out 'src="http' and run my expression then reapply what I masked out.

But before I do that I thought I would see if anyone here has any ideas.

Many thanks.

4
  • I'm still unsure what you're trying to do. Where will the images be inserted and what is the regex you've written supposed to do? Commented Jun 7, 2012 at 13:27
  • This regex looks like you just blindly copy-pasted from the one above. I would read up on javascript RegExp and play around with this. regexpal.com Commented Jun 7, 2012 at 13:28
  • The first expression identifies urls and I later use it to make those urls into hyper links. There are no images to be inserted using the expression, as I said I want to ignore already encoded images. If you look carefully you will see that the second expression has (?!src=") at the beginning... Commented Jun 7, 2012 at 13:30
  • Tronbabylove, it appears you blindly commented without taking a good look ;) Commented Jun 7, 2012 at 13:31

1 Answer 1

1
(?!src=")

is a negative lookahead, what you want there is a lookbehind, which javascript does not support.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh ok thanks, at least I learnt something new today. I will use my masking idea then. Thanks for your reply :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.