preg_match in JavaScript?
Asked Answered
A

8

87

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.

Alysonalysoun answered 20/7, 2010 at 14:56 Comment(0)
H
137

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];
Hostage answered 20/7, 2010 at 15:6 Comment(1)
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]);Gettings
C
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.

Cooke answered 20/7, 2010 at 15:6 Comment(0)
T
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.

Talkington answered 8/1, 2013 at 10:15 Comment(4)
You can also shorten this with /\[(\d+)\]\[(\d+)\]/.test(text)Epiphragm
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 ;)Grafton
Using the RegExp class constructor has the benefit that if you need to interpolate a variable in the pattern, it takes a string!Mulkey
Only works for me when escaping backslashes, like '\[(\\d+)\]\[(\\d+)\]'Linearity
M
6

This should work:

var matches = text.match(/\[(\d+)\][(\d+)\]/);
var productId = matches[1];
var shopId = matches[2];
Malformation answered 20/7, 2010 at 15:6 Comment(0)
A
4
var myregexp = /\[(\d+)\]\[(\d+)\]/;
var match = myregexp.exec(text);
if (match != null) {
    var productId = match[1];
    var shopId = match[2];
} else {
    // no match
}
Andesine answered 20/7, 2010 at 15:7 Comment(0)
I
1

get matched string back or false

function preg_match (regex, str) {
  if (new RegExp(regex).test(str)){
    return regex.exec(str);
  }
  return false;
}
Indulgent answered 11/7, 2020 at 17:10 Comment(0)
U
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""
Undoubted answered 9/11, 2019 at 13:14 Comment(0)
W
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}$","[email protected]"))

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

Whoa answered 31/5, 2020 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.