Reputation: 13
I am trying to create a JavaScript program to replace certain patterns of text with links. However due to some of the patterns existing within a URL on the page it blocks the URL links.
I am specifically looking to exclude the pattern if it is contained within a URL so for example here is my current Regex code.
$els.replaceText(/(\bX00[A-Z0-9]{7}\b)/gi, '<span class="context context_ident">$1<\/span>');
Some Example Text:
item :X00132BhJk
www.domain.com/X00132BhJk
www.domainsearch.com/search?ident=X00132BhJk
X00132BhJk
X00132BhJk
The Italic References should be selected and replaced however the references contained within the domain should not. The issue I have been having is when the reference.
Initially I tried \sX00[A-Z0-9]{7}\s
but when the reference appears on the far left of the page (First word in the sentence) it doesn't get selected. Equally so it does not select if a full stop follows or a colon precedes.
Is there a way to specifically exclude URL's by excluding / ? and = from being the immediate preceding character but select in all other cases?
Upvotes: 1
Views: 2360
Reputation: 12389
Capture (^
start |
OR [^/?=]
in a negated character class the ones, that must not appear before)
/(^|[^\/?=])(\bX00[A-Z0-9]{7}\b)/gi
And replace with: '$1<span class="context context_ident">$2</span>'
Also see fiddle; SO Regex FAQ;
Upvotes: 1
Reputation: 174696
You don't need to escape the frontslash in the closing span
tag on the replacement part.
Regex:
^((?:(?![\/?]).)*)(X00[A-Z0-9a-z]{7})(.*)$
Replacement string:
$1<span class="context context_ident">$2</span>$3
Upvotes: 0
Reputation: 4339
You can try with non-capturing parentheses (?:)
, in your case (?:[^/?=]|^)
replace(/(?:[^/?=]|^)(\bX00[A-Z0-9]{7}\b)/gi, '<span class="context context_ident">$1<\/span>');
Upvotes: 0
Reputation: 67968
(?!^www.*?X00[A-Z0-9]{7}.*$)^(.*?)(X00[A-Z0-9]{7})(.*)$
Try this.
Replace with.
\1<span class="context context_ident">$1<\/span>\2
See demo.
http://regex101.com/r/oC3nN4/7
added an m flag as well for multiline match as i have used anchors.
Upvotes: 0