Get the word under the mouse pointer

according to this ( get the word under the cursor using JavaScript ) link I can get the word under the mouse pointer.it is great for English. i change it (for arabic)

<p>ุณู„ุงู… ุจู‡ ู‡ู…ู‡</p>
Word: <span id="word"></span>

<script type="text/javascript">
    $(function() {
        // wrap words in spans
        $('p').each(function() {
            var $this = $(this);
            $this.html($this.text().replace(/[^\x00-\x80]+/g, "<span>$1</span>"));
        });

        // bind to each span
        $('p span').hover(
            function() { $('#word').text($(this).css('background-color','#ffff66').text()); },
            function() { $('#word').text(''); $(this).css('background-color',''); }
        );
    });

but it returns '$ 1' for each word. please, help!

+1
source share
1 answer

You need parentheses that appear in the original regular expression. In regular expression notation, parentheses form a โ€œmatching groupโ€, which is replaced by โ€œ $1โ€ in the replacement string.

$this.html($this.text().replace(/([^\x00-\x80]+)/g, "<span>$1</span>"));

- $1 .

, , ( $1, $2, ..).

+3

Source: https://habr.com/ru/post/1661419/