0

I have two replace methods called on a string, and I know they can be combined. I would like to know how to combine the two expressions, why, and maybe links to more information

var key = templateKey.replace(/:/g, '').replace(/\//g,'')
Theodore Enderby
  • 622
  • 1
  • 6
  • 19

2 Answers2

2

Well, Dr. Google can give you lots of links for more information on regular expressions, but in this case you're just replacing either a colon (:) or a slash (/), so you could combine them using the OR (|) grouping:

/(:|\/)/
Stephen Thomas
  • 13,843
  • 2
  • 32
  • 53
  • So much information on google for regex! It's been a real challenge for me to pick up. The "Or" operator and grouping is exactly the information I wanted to know! exp +50, regex lvl ++. The "more resources" request was in hopes someone had an idiots guide to regex basics laying around. – Theodore Enderby Dec 14 '14 at 00:52
  • btw, ateich's answer is also correct (it gets an upvote from me). – Stephen Thomas Dec 14 '14 at 01:47
1
var key = templateKey.replace(/[:\/]/g, '');
ateich
  • 520
  • 3
  • 10