1

according this (get a word under cursor using JavaScript )link i can get word under mouse pointer.it's fine for english language. i change it (for arabic language)

<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 return '$1' for each word. please help!

Community
  • 1
  • 1
KF2
  • 9,887
  • 8
  • 44
  • 77

1 Answers1

3

You need the parentheses that appear in the original regular expression. In regular expression notation, parentheses form a "match group" which is substituted in for the "$1" in the replace string.

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

Without any match groups in your regex, the $1 is simply treated as a literal dollar sign and one.

When you have multiple parenthesized match groups, the groups are used in to replace dollar-sign-denoted numbered placeholders in the order that the match groups are opened (the first match group replaces $1, the second replaces $2, etc).

apsillers
  • 112,806
  • 17
  • 235
  • 239