0

i recently started working with Javascript coming from VBA i would like to know if there is an equivalent to vba's "like" and "*" expressions.

Example:

If test Like "01##############10*" Then
braX
  • 11,506
  • 5
  • 20
  • 33

2 Answers2

1

Yes, you're looking for regular expressions. For instance, if # means "digit" and * means "anything" in your example, the regex would be /^01\d{13}10/, e.g.:

if (/^01\d{13}10/.test(test)) {

Which says:

  • ^ - Match start of string ("start assertion" or "start anchor")
  • 01 - Match 01 literally
  • \d{13} - Match a digit (\d) exactly 13 times
  • 10 - Match 10 literally

The absense of a $ anchor means we don't care what follows after the above.

Regular expressions are objects with methods. One of those methods, test, checks to see if a string matches the expression, one of the most basic operations available. There's also exec (find the first/next match). Coming at it from the other side, strings also have a match method which accepts a regular expression and looks for matches within the string.

More on regex:

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

JavaScript has RegEx. With RegEx you can try to build anything like "Like" or "*".

In general - unfortunately anything that is available in VBA is available in the other top languages. The vice versa is not always the case.

This is how to search for vi*:

var myStr = "vityata";
if (myStr.match(/vi.*/)) {
  // your code here
}

A little more here: Emulating SQL LIKE in JavaScript

Vityata
  • 42,633
  • 8
  • 55
  • 100