dennismonsewicz
dennismonsewicz

Reputation: 25552

Figuring out Regex pattern

I am still not all that good when it comes to writing Regex patterns and am having issues with trying to figure out a search pattern for the following string:

{embed_video('RpcF9EYXZpZFBhY2tfRklOQUwuZj','575','352','video_player')}

I basically need to search a page for anything in between the hash {} marks.

I have tried this:

string = $(".content").text();
string.match("^{[\w-]}");

But its not working... any ideas on what I could be doing wrong?

Thanks for the help everybody! This is what I did to make it work:

$("div", "html, body").each(function(){
        var text = $(this).text();

        if(text.match("^\{.*\}$")) {
            console.log("FOUND");
        }
    })

Upvotes: 2

Views: 103

Answers (6)

Andy E
Andy E

Reputation: 344753

One problem is the lack of a quantifier. As it stands, your regex is looking for a single \w or - character, denoted by your character class. You're probably looking for either of the following quantifiers:

  • [\w-]* - match 0 or more \w or - characters
  • [\w-]+ - match 1 or more \w or - characters

Another problem is the restrictions in the character class. [\w-] won't match (, ), ", spaces or other non-word characters that may appear. If you want to match all characters, use .. If you want to match all characters except }, use [^}] instead.

For example:

string = $(".content").text();
string.match("^{[^}]+}");

Using * would allow the content within the braces to be empty.

Side note: It looks to me like you're gearing up to eval() the code contained within the { and }. eval() is generally best avoided (if possible) for both security and performance reasons. In your case, you may be able to use this instead:

var string = $(".content").text(), fn, args;
if (string.charAt(0) == "{" && string.charAt(string.length - 1) == "}") {
    fn = string.slice(1, string.indexOf("("));
    args = string.slice(string.indexOf("("), string.lastIndexOf(")")).split(",");

    window[fn].apply(null, args);
}

Upvotes: 2

nemophrost
nemophrost

Reputation: 567

This should find the innermost content of curly braces (even nested ones).

string.match(/\{([^\{\}]*)\}/)[1]; // the [1] gets what is within the parentheses.

edit:

Thanks to the comments below here is a cleaner version:

string.match(/\{(.*?)\}/)[1];

Upvotes: 3

detunized
detunized

Reputation: 15299

Try "\{.*?\}". But it won't handle the situation with nested curly braces. Here you can test your regexps online.

string.match("^\{(.*?)\}$")[1];

Upvotes: 1

Aidas Bendoraitis
Aidas Bendoraitis

Reputation: 4003

Search for the following regular expression:

var sRe = /\{([^\}]*)\}/g;
sText.match(sRe);

It means that you are searching for character "{" followed by any symbols but not "}" optionally and then ending with "}".

Upvotes: 1

If you are using eclipse by any chance, there is a regular expression plugin with which you can play around and see how your regular expression searches your text.

I would try this

string.match("^\{.*\}$");

Upvotes: 1

hvgotcodes
hvgotcodes

Reputation: 120308

I think you need to escape the {} characters...they have special meaning in regex...

Upvotes: 0

Related Questions