Replace matching keywords outside html tags and anchor (a) tag text using regex


Shahab

I am developing an asp.net application. I want to add a keyword linking system.

I want to make a keyword hyperlink to another page. However, I should not link to that keyword if it is currently linking to any page. E.g:

it is a <a href="http://www.somesite.com">linked keyword</a> and it should be a linked keyword.

should convert to:

it is a <a href="http://www.somesite.com">linked keyword</a> and it should be a linked <a href="http://newlycreatedLink.com">keyword</a>.

As you can see, the first keyword should remain the same.

Can you help me with this problem?

I found this link in the asp.net forum . But I should adjust the answer to exclude the currently linked keyword. I searched everywhere but found nothing.

Johnny 5

To check if a keyword is "outside", look ahead

  • (?=<tagIf there is a beginning or end after the keyword$
  • [^<>]*any number of characters, not >or<
  • followed by (?:<\w|$)where \wis the shorthand for literal symbols[a-zA-Z_0-9]

So the schema might look like this:

String pattern = @"(?i)\bkeyword\b(?=[^<>]*(?:<\w|$))";

String replacement = @"<a href=\"http://newlycreatedLink.com\">\0</a>";

Put keywords into word boundaries \band use the (?i)i modifier for case sensitivity.

So this will only replace the keywordopening or closing tag.


Update : keywordAlso replace "internal" tags that don't end with </aadd |<\/[^a]:

String pattern = @"(?i)\bkeyword\b(?=[^<>]*(?:<\w|<\/[^a]|$))";

Related


PHP regex to replace keywords in text but not inside anchor tags

Mintaras I'm trying to implement automatic hyperlink functionality for keywords. The problem I'm having is that keywords can be part of other keywords. For example: potatoes, sweet potatoes. The function must know not to hyperlink potatoes in sweet potatoes..

Replace DOM text with anchor tags

virtual logic I am searching the html body, but the markup is displayed in plain text. How can I correct the anchor to replace John with the name as well as the link to show the clickable anchor. Can the replacement of search terms be solved in javascript? Inc

Replace DOM text with anchor tags

virtual logic I am searching the html body, but the markup is displayed in plain text. How can I correct the anchor to replace John with the name as well as the link to show the clickable anchor. Can the replacement of search terms be solved in javascript? Inc

Replace DOM text with anchor tags

virtual logic I am searching the html body, but the markup is displayed in plain text. How can I correct the anchor to replace John with the name as well as the link to show the clickable anchor. Can the replacement of search terms be solved in javascript? Inc

Replace DOM text with anchor tags

virtual logic I am searching the html body, but the markup is displayed in plain text. How can I correct the anchor to replace John with the name as well as the link to show the clickable anchor. Can the replacement of search terms be solved in javascript? Inc

Replace DOM text with anchor tags

virtual logic I am searching the html body, but the markup is displayed in plain text. How can I correct the anchor to replace John with the name as well as the link to show the clickable anchor. Can the replacement of search terms be solved in javascript? Inc

Extract text outside HTML tags using jQuery

Brosch Lets say I have <a href="#">My Link <span>And More</span></a>the following markup :My Link<span> Wolfe Use nodeType to filter it out: var txt = $('a').contents().filter(function(){ return this.nodeType === 3; }).text(); -Demo-

Extract text outside HTML tags using jQuery

Brosch Lets say I have <a href="#">My Link <span>And More</span></a>the following markup :My Link<span> Wolfe Use nodeType to filter it out: var txt = $('a').contents().filter(function(){ return this.nodeType === 3; }).text(); -Demo-

How to replace HTML in tag tags using jQuery

Ferens I have a labeland selectdropdown list: <label for="serviceID[1]">Service</label> <select name="serviceID[1]" id="serviceID[1]" class="jq__pickedNewService"> <option value="" selected="selected">No Service Selected</option> <option value="004">Service 1<

How to replace HTML in tag tags using jQuery

Ferens I have a labeland selectdropdown list: <label for="serviceID[1]">Service</label> <select name="serviceID[1]" id="serviceID[1]" class="jq__pickedNewService"> <option value="" selected="selected">No Service Selected</option> <option value="004">Service 1<

Java Regex get text from HTML anchor (<a>...</a>) tags

BeginnerPro: I am trying to get text inside a specific tag. So if I have: <a href="http://something.com">Found<a/> I want to be able to retrieve Foundtext. I am trying to use regular expressions. I can do it if it <a href="http://something.com>stays the same,

Java Regex get text from HTML anchor (<a>...</a>) tags

BeginnerPro: I am trying to get text inside a specific tag. So if I have: <a href="http://something.com">Found<a/> I want to be able to retrieve Foundtext. I am trying to use regular expressions. I can do it if it <a href="http://something.com>stays the same,

Java Regex get text from HTML anchor (<a>...</a>) tags

BeginnerPro: I am trying to get text inside a specific tag. So if I have: <a href="http://something.com">Found<a/> I want to be able to retrieve Foundtext. I am trying to use regular expressions. I can do it if it <a href="http://something.com>stays the same,

Replace anchor tags with other text using only CSS

Deadman I just want to replace anchor tags with some different text using CSS. I've tried using CSS pseudo-elements, but using CSS pseudo-elements puts new text into unwanted anchor tags. I want to replace the entire anchor tag so that the new text will not ha

Replace anchor tags with other text using only CSS

Deadman I just want to replace anchor tags with some different text using CSS. I've tried using CSS pseudo-elements, but using CSS pseudo-elements puts new text into unwanted anchor tags. I want to replace the entire anchor tag so that the new text will not ha

How to replace text between tags using regex

Skyler I want to replace some text in a string representing a div tag which may or may not contain style and class attributes. E.g, var s = "<div style='xxx' class='xxx'>replaceThisText<div> If it's just labels, I believe I can do this: str = str.replace(/<di

How to replace anchor tag text using C#?

Balgav I am trying to replace anchor tag value using regex Regex.Match(link, @"<a [^>]*>(.*?)</a>").Groups[1].Value.ToString();This gives me the anchor tag text. I tried using "Regex.replace" but I can't replace the exact text. example: var link="<a href="">Sa

How to replace anchor tag text using C#?

Balgav I am trying to replace anchor tag value using regex Regex.Match(link, @"<a [^>]*>(.*?)</a>").Groups[1].Value.ToString();This gives me the anchor tag text. I tried using "Regex.replace" but I can't replace the exact text. example: var link="<a href="">Sa

Regex to remove anchor tag if it's not outside myclass

Ramesh I want to use regex to remove anchor tag from a given string if it's not in my class scope. enter: <p>Hi Hello <a href="#">World</a></p>. This is <div class="myclass">testing <a href="#">content</a>. some more content</div>. One more <a href="#"> Link <