Match jQuery function, how to get the cards

Asked

Viewed 939 times

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 []

2 answers

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

You are not signed in. Login or sign up in order to post.