André Figueira
André Figueira

Reputation: 6696

Ignore @mentions wrapped in links

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

Answers (2)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

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>

enter image description here

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

Andr&#233; Figueira
Andr&#233; Figueira

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

Related Questions