0

I'm new to the forum. Many Thanks to "stackoverflow" and you for the opportunity.

I have a problem. How I can pass a variable to a regular expression to split?

I have this:

var text = "I am new to the forum";
var term = "TO";

var textSplit = text.split(/term/gi);

What I like to do is to pass the variable "term" to the regular expression so you can divide if no match, but not how. Any idea better.

Thank you!

2 Answers2

2

use the regex object and try this:

var modifiers = "gi"
var patt = new RegExp(term,modifiers);
var textSplit = text.split(patt);
Mortalus
  • 10,574
  • 11
  • 67
  • 117
1
var term = /TO/gi;
var textSplit = text.split(term);
srubin
  • 259
  • 2
  • 9