0

I'd like to replace character "@" by "(at)" within all span tags that contain @ character. Is there anything like $('span:contains("@")').text().replace ?

0

1 Answer 1

3

The code snippet that you have posted is one of the options. But you need to reset the text content of the element.

$('span:contains("@")').text(function(_, text) {
     return text.replace(/@/g, '(at)');
});

Here is a non-jQuery alternative:

[].forEach.call(document.querySelectorAll('span'), function(span) {
    if ( span.textContent.includes('@') ) {
       span.textContent = span.textContent.replace(/@/g, '(at)');
    }
});

Please note that resetting .textContent can be destructive if the element has child elements. In that case you can reset the html content of the element (.innerHTML) or better, filter the text nodes of the element.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.