1

I am writing as SQL query which might return HTML text also. HTML Tags are fine for me, because I want to show it formatted with the HTML tags in the front end. But I do not need links. I mean is there anyway I can strip off the hyper links only from the column. just the anchor tag. I am so bad in Regular Expressions, though i think that might be the solution for this. Any help!

1 Answer 1

3

This should work fine for links:

<a[^>]*>(.*?)<\/a>

Since you say you don't understand regular expressions, I might as well explain. The <a part is straightforward, the [^>]* will match anything up to the closing bracket, the bracket is just the bracket. (.*?) matches anything, regardless of length, empty links as well. The ? is required so that it becomes non-greedy, so it stops at the first closing tag. <\/a> matches the closing tag.

Edit: if you have spaces in between your tags, you can use <a[^>]*>((?:.|\s)*?)<\/a>. Notice I added the (?:.|\s)*? in place of .*?. The .|\s means match any character or space, the ?: indicate a non capturing group, since we don't care which particular character was matched.

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

7 Comments

So Pavlin, this in fact matches the string from the start of the anchor tag to the end of the closing tag, am i right?
Yes, that's right. If you want to remove the tags, just do a replace with the first capture group. The parentheses define the capturing group.
Bingo it worked like hell.. :) I did a two replace one for starting and next for the ending tag.. .:)
Hey Pavlin, one more question, the reg ex is failing if i have new line character in between the text. any solution for that :(
I edited my answer, it should handle spaces in between the link tags now.
|

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.