Using RegExp in JavaScript to simulate a wildcard search

0

Good day, I have a problem, I'm working with JavaScript, I have a function that runs through an array, but I need you to find matches within the array, for example, in the text "Carlos Daniel Zárate Ramírez" find "Carlos Zarate", which imitate the like function of SQL, in SQL just enough to do like '% Carlos% Zarate%', researching I found that it can be done with regular expressions, but I have not yet achieved it. I hope you can help me. Thank you very much.

    
asked by Carlos Daniel Zárate Ramírez 14.12.2018 в 17:13
source

1 answer

1

In a regular expression, the dot . means "any character" (but only one) and the asterisk * means "any number of repetitions of the character mentioned above".

Therefore .* becomes the wild card. In your case .*Carlos.*Zarate.* Careful that "any number of repetitions" also implies zero, so "CarlosZarate" would be accepted. If you want there to be at least one character between "Carlos" and "Zarate" you can use .*Carlos.+Zarate.* , since + means "one or more repetitions of the previous character.

    
answered by 02.01.2019 в 16:43