maria maria - 12 days ago 5
Javascript Question

javascript regex search and find

Im trying to check if a string contains [suggestion=mixofnumberandcharacter]

But im not sure how to do that, anynumber will of course be any number or character.

and, how do i after that get the suggestion mixofnumberandcharacter id?

Answer

What about:

var str = "asd asd asd asd [suggestion=mix0fnumb3r4ndch4racTeR] sd  asd asd sd";
var found = str.match(/\[suggestion=([0-9A-Za-z]*)\]/);
console.log(found);

The console output will be:

Array[2] {
    0        : "[suggestion=mix0fnumb3r4ndch4racTeR]"
    1        : "mix0fnumb3r4ndch4racTeR"
    index    : 16
    input    : "asd asd asd asd [suggestion=mix0fnumb3r4ndch4racTeR] sd  asd asd sd"
    length   : 2
    __proto__:    Array[0]
}

So what you are looking for is found[1] which is matched by the (). Fiddle here.

Comments