English 中文(简体)
jquery match() variable interpolation - complex regexes
原标题:

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>

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

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.

In Perl I would do something like:

if ( $rcpt =~ /^QrcptE/ )
{
    # code
}

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

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

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

最佳回答

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(/
?
/);

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.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签