Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add variable inside regex in javascript

var pattern="cd"
var repeats=3
new RegExp(`${pattern}{${repeats}}`, "g") 
Comment

js regex with variable

function alphabet(str) {
  let alphabetStr = 'abcdefghijklmnopqrstuvwxyz';
  let strRegEx =`[${str[0]}-${str[3]}]`;  //set regex with variables
  let newRegEx = new RegExp(strRegEx, 'g'); //create new regex object
  //regex object pattern is: RegExp(pattern, modifiers)
  let charSetStr = alphabetStr.match(newRegEx);  
  return charSetStr;
}

console.log(alphabet('abce'));
//expected [ 'a', 'b', 'c', 'd', 'e' ]
Comment

javascript use variable regex

// CREATE A REGEXP object
var replace = "regex";
var re = new RegExp(replace,"g");
//then use
"mystring".replace(re, "newstring")
Comment

PREVIOUS NEXT
Code Example
Javascript :: repeat react component n times 
Javascript :: isempty is not defined 
Javascript :: get all local storage 
Javascript :: get element or class 
Javascript :: javascript transpose array 
Javascript :: Module Error (from ./node_modules/eslint-loader/dist/cjs.js): 
Javascript :: click outside element jquery 
Javascript :: prime or not in javascript 
Javascript :: install gulp ubuntu 20.04 
Javascript :: react native linking email 
Javascript :: how to make a modal stay center of screen 
Javascript :: console log object js 
Javascript :: how to hide url parameters in address bar using javascript 
Javascript :: javascript reload page 
Javascript :: spring rest api cors error in react app 
Javascript :: javascript set element width 
Javascript :: how to always run validators mongoose 
Javascript :: jquery is check 
Javascript :: nodejs on exit event 
Javascript :: append to top javascript 
Javascript :: jquery serialize form data to object 
Javascript :: javascript recursive sum function 
Javascript :: update angular materia; 
Javascript :: how to run nextjs in another port 
Javascript :: how to run angular application in visual studio code 
Javascript :: close tab using jquery 
Javascript :: use import in node 
Javascript :: examples of toastr in jquery 
Javascript :: search class regex 
Javascript :: axios async get 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =