The Mozzila website provides an example with regular expressions:

var re = /(\w+)\s(\w+)/; var str = 'Джон Смит'; var newstr = str.replace(re, '$2, $1'); console.log(newstr); 

It is said that the example will print "Smith, John." But in my browsers the latest versions of chrome and firefox persist John Smith. What I do not understand, please explain, or do something wrong?

    3 answers 3

    I will add the next answer:

    If you go down a little lower on the page by the link in the question, to the description of special characters you can see the description for the \w

    Matches any alphanumeric character from the basic Latin alphabet, including the underscore character .
    Equivalent to the character set [A-Za-z0-9_] .

    As you can see, there are no Russian letters here. Consequently, there is an erroneous translation.

    • @ D-side, corrected the example there - Grundy
    • @ D-side, fixed :-) - Grundy

    The case in Cyrillic - \ w does not recognize it. This is how it will work:

     var re = /(\w+)\s(\w+)/; var str = 'John Smith'; var newstr = str.replace(re, '$2, $1'); console.log(newstr); 

    And so too:

     var re = /([а-яА-Я\w]+)\s([а-яА-Я\w]+)/; var str = 'Джон Смит'; var newstr = str.replace(re, '$2, $1'); console.log(newstr); 
    • Ёё missed. - Grundy
    • The main letter in Russian is e. The whole point if necessary falls on it (c) Bricks - Darth

    translation errors. On the English site written

     var str = "John Smith"; 

    the symbol \ w itself is equivalent to [A-Za-z0-9_] (that is, only Latin letters, numbers, and underscores)