Getting whole words with Regexp in Jquery

Asked

Viewed 198 times

3

$.fn.wrapInTag = function(opts) {

    var tag = opts.tag || 'strong',
        words = opts.words || [],
        regex = RegExp(words.join('|'), 'gi'),
        replacement = '<' + tag + '>$&</' + tag + '>';

    return this.html(function() {
        return $(this).text().replace(regex, replacement);
    });
};

$('p').wrapInTag({
    tag: 'em',
    words: ['html', 'CSS', 'JavaScript'],
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<p>O HTML d é muito usado nos dias de hoje, juntamente com o HTML usamos JavaScript e CSS...</p>

I’m having problems with Regexp in Jquery, I have a list of words that it takes from <p> and puts inside the <em>. It works almost right, however it does not respect whole words, if I look for HTML it returns HTML1, HTMLX, HTMLZ and not only HTML, if the HTML is along with other words it identifies the word also in the middle of others, it does not respect an entire word.

What I seek is that:

  • "HTML" stay: HTML
  • "Htmlabc" remain intact, not italicized, only: Htmlabc;

My example code: http://jsbin.com/zopilefevu/edit?html,js,output

Do the test by adding any letter in front of or behind the words html, CSS, and Javascript you can see that it continues to consider the words even though it has other characters together at the beginning or end of the word.

  • It works for me... you can explain better how to reproduce the problem?

  • Does this work as you want? https://jsfiddle.net/hrmgwjxf/ (is equal to your jsBin)

  • Hello good night, that’s how the text has the words HTML, CSS and JS. If you put another character in front of the HTML for example abcHTML, it keeps considering the HTML I would like it to respect the words since HTML is different from abcHTML.

  • ah... okay, I get it.

  • There’s a way to fix it ?

1 answer

3


You can add a denial rule to every word. It would look like this:

regex = RegExp('(\\W|^)(' + words.join('|') + ')(\\W|$)', 'gi'),

and that way you’d get a regex like this:

/(\W|^)(html|CSS|JavaScript)(\W|$)/gi

and then an important step:

replacement = '$1<' + tag + '>$2</' + tag + '>$3';

that makes it index what was captured within each of the capture groups () and, respectively, put it in the right place of the new string that will be formed.

Thus, in practice prevents a match if there is a letter or number before or after of the word you indicated.

jsFiddle: https://jsfiddle.net/m4dekxq4/

regex: https://regex101.com/r/lEP8DX/2

  • 1

    Our friend thank you so much :-) Works exactly as I wanted !!!

  • 1

    @Patrickalima made an improvement suggested by @bacco, thus allowing you to have one of the keywords at the beginning or end of the tag text.

  • I took a look at the modifications already thank you, they were very good, I liked, really a good idea of Stock. I’m trying to get the text from a textarea with $('textarea'). val(); and pass this text as a variable for this function, but I’m not getting kkkkk. I’m a bit of a beginner in the subject. Without wanting to abuse too much you could tell me how I can change the current function? I thank you :-). I’m going to include a few more words and I’d like to use the last method on line 12 of the jsfiddle that separates them as if it were an array, so it looks for the words.

  • @Patrickalima adapts my jsFiddle to the code you have that doesn’t work. So I can see how you’re doing and help.

  • Hello Sergio good morning, I want to get the text of a textarea via keydown, pass this text to a variable and trigger Regexp to replace the italic words and insert the text in a div to show the result, but I am not able to assemble this part to pass as variable the text obtained for Regexp. I left in the form of a comment the doubts I have. Here is my project: https://jsfiddle.net/patrickalima98/kqmc9fp7/3/ Thanks in advance for your help and attention :-).

  • @Patrickalima what you need then is a modified version of what you have, to receive a string and reponder with formatted html... that’s it?

  • Hello Sergio good night, That’s right I needed a function to do this. I have been researching and breaking my head a little with this function and found that it is an object that holds these words kkk. Sailor of first trip kkkkk. Good after two days I managed to assemble the function as I wanted. I will leave it here if anyone needs it: https://jsfiddle.net/patrickalima98/1kw7wrnp/3/ Thank you very much for your help, attention and excellent response :-)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.