Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 :: toggle checkbox in javascript 
Javascript :: swap key value object javascript 
Javascript :: stackoverflow narrate text js 
Javascript :: install react router 
Javascript :: how to see if the window has focus in js 
Javascript :: prevstate in usestate 
Javascript :: error handling in express 
Javascript :: regex to indentify url 
Javascript :: es6 map usin index 
Javascript :: how to wait a determined amount of time before doing something in js 
Javascript :: javascript backslash 
Javascript :: javascript autoscroll 
Javascript :: E: Unable to locate package npm 
Javascript :: send data from one page to another html page in Javascript 
Javascript :: js convert html to text 
Javascript :: js remove item from array by value 
Javascript :: get value of hidden field jquery 
Javascript :: react js create element 
Javascript :: javascript validate password 
Javascript :: jquery get name attribute 
Javascript :: import typography react 
Javascript :: calculato 
Javascript :: put 0 in front of month number javascript 
Javascript :: send message whatsapp javascript 
Javascript :: set date to input date js 
Javascript :: Find all links / pages on a website 
Javascript :: async react setstate 
Javascript :: javascript remove final newline from string 
Javascript :: javascript sort array by object property 
Javascript :: open modal js 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =