Reputation: 6696
Hi I've got a script that parses @mentions into links, but this needs to be loaded more than once and currently it breaks things it has already parsed as links so i need to get it to ignore @mentions already wrapped in a tags but I can't see to get it working.
This is what I have so far:
/((?:^|[^a-zA-Z0-9_!#$%&*@@]|RT:?))([@@])([a-zA-Z0-9_\.]{1,20})(\/[a-zA-Z][a-zA-Z0-9_-]{0,24})?/g
An example string is:
Hi @andre Hi <a href="">@jane</a>
@andre
should be wrapped in the a tag like @jane
is, and @jane
is already in a tag <a href="">@jane</a>
and should be ignored.
Upvotes: 0
Views: 103
Reputation: 15000
This regex will capture the naked @names
which are not already wrapped inside like this <a href="">@jane</a>
Regex (?!>)([@@])([a-zA-Z0-9_\.]{1,20})\b(?!<)
Replace with <a href="">$0</a>
Input text: Hi @andre Hi <a href="http://some.url">@jane</a>
Output text: Hi <a href="">@andre</a> Hi <a href="http://some.url">@jane</a>
Upvotes: 1
Reputation: 6696
The reason for this question was because I was converting @mentions to links on the fly, and I was loading additional content which had @mentions to replace. I was running a linkify function to link every match which was causing issues.
I couldn't get the provided answers in the comments to work so I tried a different approach.
I know where the additional content was being added (before a static button) so I used the jquery slice function to select what elements to run the function on.
var linkify = $('.user-profile-feed-item').slice(-4).find('.linkify');
This way also the function only runs for what is needed without having to resort to additional regex filtering.
Upvotes: 0