Question

I have been struggling with this issue for quite some time, the code works fine with latin chars, as for cyrillic, jquery don't recognize it at all.

$('p').each(function() {   var $this = $(this);
    $this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));  
});

I have also tried the following code, but for some reason, jQuery skips every second word and doesn't wrap it :(

$(this).html($(this).text().replace(/\s([a-zA-Zа-яА-ЯёЁ]+)\s/g, " <span> $1 </span> "));

Any hint?

No correct solution

OTHER TIPS

It appears JS isn't so good at this (as noted here by @Rolice in the comments above). In most languages, you merely need to change the locale.

You may have to do something like this:

replace(/(^|[^\w\u0400-\u04FF])(\w+)([^\w\u0400-\u04FF]|$)/,
  "$1<span>$2</span>$3");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top