84

Is it possible in JavaScript to do something like preg_match does in PHP ?

I would like to be able to get two numbers from string:

var text = 'price[5][68]';

into two separated variables:

var productId = 5;
var shopId    = 68;

Edit: I also use MooTools if it would help.

hsz
  • 148,279
  • 62
  • 259
  • 315

8 Answers8

134

JavaScript has a RegExp object which does what you want. The String object has a match() function that will help you out.

var matches = text.match(/price\[(\d+)\]\[(\d+)\]/);
var productId = matches[1];
var shopId    = matches[2];
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
godswearhats
  • 2,155
  • 1
  • 14
  • 6
  • 33
    For other googlers; `text.match` will return the result of the matching. so `var match = text.match(/price\[(\d+)\]\[(\d+)\]/)` and then `alert(match[1]);` – Maurice Sep 27 '12 at 14:45
33
var text = 'price[5][68]';
var regex = /price\[(\d+)\]\[(\d+)\]/gi;
match = regex.exec(text);

match[1] and match[2] will contain the numbers you're looking for.

kander
  • 4,226
  • 1
  • 22
  • 43
24
var thisRegex = new RegExp('\[(\d+)\]\[(\d+)\]');

if(!thisRegex.test(text)){
    alert('fail');
}

I found test to act more preg_match as it provides a Boolean return. However you do have to declare a RegExp var.

TIP: RegExp adds it's own / at the start and finish, so don't pass them.

Tracey Turn
  • 598
  • 4
  • 15
  • 7
    You can also shorten this with `/\[(\d+)\]\[(\d+)\]/.test(text)` – 0x6C77 May 07 '13 at 15:35
  • I agree, as I was looking for how to reproduce the regex testing feature of preg_match when I saw the title of this question ;) – flu Oct 15 '13 at 17:10
  • Using the RegExp class constructor has the benefit that if you need to interpolate a variable in the pattern, it takes a string! – Nathaniel Rogers Jul 26 '17 at 04:34
  • Only works for me when escaping backslashes, like '\\[(\\d+)\\]\\[(\\d+)\\]' – n.r. Oct 29 '19 at 10:49
6

This should work:

var matches = text.match(/\[(\d+)\][(\d+)\]/);
var productId = matches[1];
var shopId = matches[2];
Dan Stocker
  • 702
  • 7
  • 5
4
var myregexp = /\[(\d+)\]\[(\d+)\]/;
var match = myregexp.exec(text);
if (match != null) {
    var productId = match[1];
    var shopId = match[2];
} else {
    // no match
}
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

get matched string back or false

function preg_match (regex, str) {
  if (new RegExp(regex).test(str)){
    return regex.exec(str);
  }
  return false;
}
dazzafact
  • 2,570
  • 3
  • 30
  • 49
0

Sample code to get image links within HTML content. Like preg_match_all in PHP

let HTML = '<div class="imageset"><table><tbody><tr><td width="50%"><img src="htt ps://domain.com/uploads/monthly_2019_11/7/1.png.jpg" class="fr-fic fr-dii"></td><td width="50%"><img src="htt ps://domain.com/uploads/monthly_2019_11/7/9.png.jpg" class="fr-fic fr-dii"></td></tr></tbody></table></div>';
let re = /<img src="(.*?)"/gi;
let result = HTML.match(re);

out array

0: "<img src="htt ps://domain.com/uploads/monthly_2019_11/7/1.png.jpg""
1: "<img src="htt ps://domain.com/uploads/monthly_2019_11/7/9.png.jpg""
0

Some Googling brought me to this :

function preg_match (regex, str) {
  return (new RegExp(regex).test(str))
}
console.log(preg_match("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$","test"))
console.log(preg_match("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$","what@google.com"))

See https://locutus.io for more info.

Community
  • 1
  • 1
Justin Liu
  • 581
  • 6
  • 21