javascriptregexxregexp

XRegExp replace


I'm trying to do some string manipulation on some words that may or may not have unicode characters in them.

Here's my code:

var regex = XRegExp("(\\P{L}+)", "gui");
var s = 'no sea demásiado tarde';
var ret = XRegExp.replace(s, regex, "<span>$1</span>");

console.log(ret);
<script src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js"></script>

But the words are not getting wrapped by the span tags as expected. How come?

Thank you


Solution

  • Because you are wrapping non-letters with \\P{L}+ as \P{L} matches any character other than a Unicode letter.

    Use "\\p{L}+" pattern and replace with <span>$&</span> (the $& backreference stands for a whole match).

    var regex = XRegExp("\\p{L}+", "gui");
    var s = 'no sea demásiado tarde';
    var ret = XRegExp.replace(s, regex, "<span>$&</span>");
    
    console.log(ret);
    <script src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js"></script>