JQuery match () variable interpolation - complex regular expressions

I already reviewed this one , which was useful for the point.

Here is the problem. I have a list of users that propagate to an element through a user click; something like that:

<div id="box">
    joe-user
    page-joe-user
    someone-else
    page-someone-else
</div>

In a click, I want to make sure that the user has not yet clicked on the div. So, I am doing something like:

if ( ! $('#box').html().match(rcpt) )
{
   update_div();
}
else
{
   alert(rcpt+' already exists.');
}

However, given the lack of interpolation that javascript has for regular expressions, I get my warning to run in the use-case where it is page-joe-userselected, and then the user selects joe-userwhich are clearly not exactly the same.

In Perl, I would do something like:

if ( $rcpt =~ /^\Qrcpt\E/ )
{
    # code
}

All I want to do is change my match () as:

if ( ! $('#box').html().match(/^rcpt/) )
{
    # code
}

if ( ! $('#box').html().match(rcpt) ) , . new RegExp() , RE IE $('#box').html().match(new RegExp('^'+rcpt)). $('#box').html().match('/^'+rcpt'/'). , - . javascript.

, -, , , .

+3
1

match , jQuery.

- HTML.

:

<ul id="users">
    <li>joe-user</li>
    <li>page-joe-user</li>
    <li>someone-else</li>
    <li>page-someone-else</li>
</ul>

:

if($('#users li').is(function () { return $(this).text() === rcpt; }))

-, text(), . ($('#box').text().match(...))


EDIT. HTML .

:

var userExists = false;
var users = $('#box').text().split(/\r?\n/);

for(var i = 0; i < users.length; i++) {   //IE doesn't have indexOf
    if (users[i] == rcpt) {
        userExists = true;
        break;
    }
}
if (userExists) {
    //Do something
}

: .

+2

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


All Articles