jquery match() variable interpolation - complex regexes

谁说我不能喝 提交于 2019-12-05 19:22:06

The match function only works on strings, not jQuery objects.

The best way to do this is to put each username into a separate HTML tag.

For example:

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

You can then write the following:

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

If you want to do it your way, you should call text() to get the string inside the element. ($('#box').text().match(...))


EDIT: The best way to do this using your HTML would be to split the string.

For example:

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
}

This has the added benefit of not being vulnerable to regex-injection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!