preg_match in JavaScript?

JavascriptRegexMootools

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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];

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

This should work:

var matches = text.match(/\[(\d+)\][(\d+)\]/);
var productId = matches[1];
var shopId = matches[2];

Solution 5 - Javascript

var myregexp = /\[(\d+)\]\[(\d+)\]/;
var match = myregexp.exec(text);
if (match != null) {
    var productId = match[1];
    var shopId = match[2];
} else {
	// no match
}

Solution 6 - Javascript

get matched string back or false

function preg_match (regex, str) {
  if (new RegExp(regex).test(str)){
	return regex.exec(str);
  }
  return false;
}

Solution 7 - Javascript

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""

Solution 8 - Javascript

Some Googling brought me to [this](https://raw.githubusercontent.com/kvz/locutus/master/src/php/pcre/preg_match.js "https://raw.githubusercontent.com/kvz/locutus/master/src/php/pcre/preg_match.js";) :

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhszView Question on Stackoverflow
Solution 1 - JavascriptgodswearhatsView Answer on Stackoverflow
Solution 2 - JavascriptkanderView Answer on Stackoverflow
Solution 3 - JavascriptTracey TurnView Answer on Stackoverflow
Solution 4 - JavascriptDan StockerView Answer on Stackoverflow
Solution 5 - JavascriptTim PietzckerView Answer on Stackoverflow
Solution 6 - JavascriptdazzafactView Answer on Stackoverflow
Solution 7 - JavascriptZafer BAHADIRView Answer on Stackoverflow
Solution 8 - JavascriptJustin LiuView Answer on Stackoverflow