2

I am using a small Javascript/Jquery function to inject a element into some text, if there is not already such an element.

The code looks like this:

$(document).ready(function () {
$(function () {
    $(".replaceText").html(function (i, text) {
        if ($('.replaceText > span.dbOrange').length) {
            return;
        }
        else {
            return text.replace(/\w+\s\w+/, function (match) {
                return '<span class="dbOrange">' + match + '</span>';
            });
        }
    });
});

});

This works fine in 9 out of 10 cases. The first two words are styled with orange color and bold. However, if a the the second word contains an Umlaut like ä ö ü the Regex breaks at this character and it looks like the word would end there.

Can you help me with a more failsafe solution?

Kind regards

Marco
  • 22,856
  • 9
  • 75
  • 124

3 Answers3

1

\w is a shorthand for [A-Za-z0-9_], which does not contain the umlaut character. If you want other ranges included, you'll need to create an explicit set of acceptable characters.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
1

Try this:

/\S+\s\S+/

\S matches non-whitespace characters.

eminor
  • 923
  • 6
  • 11
0

Yes this is a common issue for non english characters in Javascript regular expressions. They will not be recognized by \w. You will have to specify the unicode representation \uXXXXX for each of the special characters separately

devnull69
  • 16,402
  • 8
  • 50
  • 61