Javascript wildcard variable?

The value of product_id can be a combination of letters and numbers, for example: GB47NTQQ.

I want to check if everything except the 3rd and 4th characters matches.

Sort of:

 if product_id = GBxxNTQQ //where x could be any number or letter. //do things else //do other things 

How can I accomplish this using JavaScript?

+4
source share
4 answers

Use regex and string.match (). Periods are single wildcard characters.

 string.match(/GB..NTQQ/); 
+9
source

Use regex :

 if ('GB47NTQQ'.match(/^GB..NTQQ$/)) { // yes, matches } 
+5
source

The answers have so far been suggested by match , but test is most likely more appropriate, since it returns true or false, whereas match returns null or an array of matches, therefore a (implicit) type conversion of the result within the state is required.

 if (/GB..NTQQ/.test(product_id)) { ... } 
+2
source
  if (myString.match(/regex/)) { /*Success!*/ } 

You can find more information here: http://www.regular-expressions.info/javascript.html

0
source

Source: https://habr.com/ru/post/1391762/


All Articles