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 :: [Error - 10:52:45 PM] Failed to load jshint library 
Javascript :: use ngfor to make a dropdown in angular from array 
Javascript :: js event preventdefault continue 
Javascript :: what 1hr in milliseconds in javascript 
Javascript :: string contains substring javascript 
Javascript :: typeorm get data from a table by array of id 
Javascript :: nodejs check if string matches regex 
Javascript :: brew node switch version 
Javascript :: regex link validation 
Javascript :: smooth link to anchor js 
Javascript :: check if js property exists in class 
Javascript :: unshift 
Javascript :: fetch data from asyncstorage react native 
Javascript :: react conditional styling 
Javascript :: node js starting template 
Javascript :: using .includes for an array of objects js 
Javascript :: javascript token generator 
Javascript :: jquery edit iframe content 
Javascript :: javascript format float 
Javascript :: iterate object in react 
Javascript :: js random numbers 
Javascript :: contenteditable paste plain text 
Javascript :: users api testing 
Javascript :: arabic regex javascript 
Javascript :: basic server on node.js 
Javascript :: js get clipboard data 
Javascript :: js array to csv 
Javascript :: authfunctions 
Javascript :: jquery make new variable with dynamic name 
Javascript :: styling element using jquery 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =