jQuery match()变量插值-复杂正则表达式 [英] jquery match() variable interpolation - complex regexes

查看:107
本文介绍了jQuery match()变量插值-复杂正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了,这对您有所帮助.

I've already looked at this, which was helpful to a point.

这是问题所在.我有一个通过用户点击传播到一个元素中的用户列表;像这样的东西:

Here's the problem. I have a list of users propagated into an element via user click; something like this:

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

单击时,我要确保尚未将用户单击到div中.所以,我正在做类似的事情:

On click, I want to make sure that the user has not already been clicked into the div. So, I'm doing something like:

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

但是,由于javascript缺少正则表达式的插值功能,这导致我的警报在选择page-joe-user然后用户选择joe-user的用例中触发.

However, with existing lack of interpolation that javascript has for regular expressions, is causing my alert to trigger in the use-case where page-joe-user is selected and then the user selects joe-user, which are clearly not exactly the same.

在Perl中,我会做类似的事情:

In Perl I would do something like:

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

我要做的就是将match()更改为:

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

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

if ( ! $('#box').html().match(rcpt) )似乎有点前途,但也失败了.使用new RegExp()在功能IE $('#box').html().match(new RegExp('^'+rcpt))中使用复杂的RE语法的串联也不起作用.我也尝试过$('#box').html().match('/^'+rcpt'/').我只能想象我缺少什么.我对javascript很陌生.

if ( ! $('#box').html().match(rcpt) ) seemed a little promising but it, too, fails. Using new RegExp() also does not work using concatenation of complex RE syntax within the function IE $('#box').html().match(new RegExp('^'+rcpt)). I also tried $('#box').html().match('/^'+rcpt'/'). I can only imagine that I'm missing something. I'm pretty new to javascript.

在此站点上,我似乎找不到能够真正解决此类用例的任何东西.

I don't seem to be able to find anything that really addresses such a use-case, here on this site.

TIA

推荐答案

match函数仅适用于字符串,不适用于jQuery对象.

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

最好的方法是将每个用户名放入单独的HTML标记中.

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

例如:

<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; }))

如果要按自己的方式进行操作,则应调用text()以获得元素内的字符串. ($('#box').text().match(...))

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

编辑:使用HTML进行此操作的最佳方法是分割字符串.

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

例如:

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.

这篇关于jQuery match()变量插值-复杂正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆