0

I need regex that remove all the a tags from html and show the url as text.

for example this text:

abc <a href="http://a.com" target="_blank">bbb</a> ccccccc

will become:

abc bbb http://a.com ccccccc
2
  • Why do you "need" a regular expression to do this (imperfectly, with edge-cases) when the DOM gives you far more reliability? And why does the href ("http://a.com") come between bbb and cccccc? Commented Aug 7, 2016 at 20:23
  • I work on node.js so I dont have dom. I nead to show all the date from html to no-html area. Commented Aug 7, 2016 at 20:29

2 Answers 2

1

You could use getElementsByTagName for "a" and getAttribute for "href" or has it got to be a RegEx?

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

3 Comments

it has got in RegEx, I'ts write in node.js so I don't have dom.
maybe this post link is helpful.
I can use also npmjs.com/package/jQuery but I prefer to use regEx or string manipulation
0
text = html.replace(/href="([^"]*)"[^>]*>([^<]*)</g, '>$2 $1<').replace(/<[^>]*>/g, '');

The first replace adds the url after the link text. The second replace removes all html tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.