1
How can I get my hands on the match function?
console.log($("COMMANDO database -> run []").match(/COMMAND\S*(.*)\S*->\S*(.*)/g)[0]);
In theory I would have to get database
, run
and what’s inside the []
1
How can I get my hands on the match function?
console.log($("COMMANDO database -> run []").match(/COMMAND\S*(.*)\S*->\S*(.*)/g)[0]);
In theory I would have to get database
, run
and what’s inside the []
5
The method Match is not Jquery, is native to the Javascript language, I modified its regular expression to pick up the contents inside the brackets.
var regex = /\S*(.*)\S*->\S*(.*)\[(.*?)\]/i;
var input = "COMMANDO database -> run [conteúdo]";
if(regex.test(input)) {
var matches = input.match(regex);
for(i = 0; i < matches.length; i++){
alert(matches[i]);
}
} else {
alert("Nenhuma combinação encontrada.");
}
1
There is no function match
jQuery library. You might want to use the function String.match
javascript native. It works like this:
"COMMANDO database -> run []".match(/COMMAND\S*(.*)\S*->\S*(.*)/)
Note that we had to remove the flag g
from regex to capture groups. Also note that the string contains COMMANDO
while the regex searches COMMAND
. In that case, there is no match.
Browser other questions tagged javascript jquery
You are not signed in. Login or sign up in order to post.
+1 to test with
.test
before! I always did the.match
and checked if the result was notnull
! This is much more elegant!– Renato Gama
To make the answer more complete I would only include the link to the documentation of
.match
on MDN– Renato Gama