I need to replace all url in string...

in #steemcodeit7 years ago (edited)

Hi,

I need to replace in javascript regex, all url in string outside any html tags and which include the domain "steemitimages.com".

I have this string:

http://google.com/ and a sister which is also a kitten http://s3.amazonaws.com/rapgenius/filepicker%2FvCleswcKTpuRXKptjOPo_kitten.jpg and lots of kittens
http://s3.amazonaws.com/rapgenius/filepicker%2FvCleswcKTpuRXKptjOPo_kitten.jpg <img src="https://steemitimages.com/0x0/https://ipfs.busy.org/ipfs/QmfEdkPtGTzv7dMm6P81FoqSQhqXxoq4KTrYPDuAicrWCP" alt=""> http://s3.amazonaws.com/rapgenius/filepicker%2FvCleswcKTpuRXKptjOPo_kitten.jpg
http://s3.amazonaws.com/rapgenius/filepicker%2FvCleswcKTpuRXKptjOPo_kitten.jpg
http://s3.amazonaws.com/rapgenius/filepicker%2FvCleswcKTpuRXKptjOPo_kitten.jpg https://steemitimages.com/0x0/https://steemitimages.com/0x0/https://gateway.ipfs.io/ipfs/QmWqwTSacujoQ9ZCSsLmDjo3ER61oZpe9jeCGRsyXbu11S

and i need to to get only the links from https://steemitimages.com/0x0 if the url reside outside a img tag.

I want to replace al url strings from this to img tag if this are not ready in one.

can help me?

Thank you

Sort:  

It seems as though you might be getting into "Attempted to parse HTML with regex; system returned Cthulhu" territory...

I don't really do regex stuff very much, granted, but if I had to do this personally, I'd just avoid regex and scan the whole string character-by-character.

What you seem to be looking for, basically, is strings beginning with https://steemitimages.com/0x0 that aren't inside HTML tags. So I think if you scan the string looking for that prefix, but any time you encounter the character <, skip to the next > you find, you'd be able to select only the instances of that prefix that you want.

There may be some odd special cases that would be problems there, but I can't think of any. As long as you have well-formed HTML, you shouldn't need to worry about nesting depth of the angle brackets or anything.

You have a minor misspelling in the following sentence:

need to replace in javascript regex, all url in string outside any html tags and wich include the domain "steemitimages.
It should be which instead of wich.