1

I have a problem I can not solve. Let's say I have a variable that keeps the regExp pattern, that is to be provided by the user:

var pattern = this.state.regExpVal;

I also have a variable that keeps the value of textInput, meaning some piece of text, eg, postal codes.

var str = this.state.textAreaVal;

I create a new regExp object:

var myRegEx = new RegExp(pattern, 'g');

And the result is not ok:(it seems that the flag search globally is not working and I can not figure out why);

var result = str.match(myRegEx);

Can anyone help?

3
  • 3
    What is the problem? Commented Mar 23, 2017 at 17:16
  • What is the string and what is the regex you are trying? Commented Mar 23, 2017 at 17:18
  • Both string and regex are to be provided by the user. Let's say that var str='34-990' and pattern for regExp=\d{2}-\d{3}. Commented Mar 23, 2017 at 17:22

2 Answers 2

1
let reg = new RegExp("super", 'g');
"SOsuperISsuper!".match(reg);

The result is an Array with 2 Elements ["super","super"] as expected.

I guess the problem lies within your this some undefined or null values or in case-sense (try include 'i')

Edit:

let myObject = {
    state: {
     regExpVal: "super"
    }
}
let reg = new RegExp(myObject.state.regExpVal, 'g');
"SOsuperISsuper!".match(reg);

Edit 2:

let myObject = {
    state: {
     regExpVal: "super"
       }
   };
let pattern = myObject.state.regExpVal;
let reg = new RegExp(pattern, 'g');
"SOsuperISsuper!".match(reg);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but I want to know whether there is an option to put variables into the pattern. var pattern = this.state.regExpVal; var str = this.state.textAreaVal; var reg = new RegExp(pattern, 'g');-----here trying str.match(reg) is not working ok. Both pattern and str are strings.
I guess maybye the problem lays somewhere else, because I have been trying all combinations. Thank you for the support.
You can use variables, but if your value, who feeds the patter variable, is changing, you have to create a new RegEx Object.
insert a debugger; statement and inspect your variables at runtime. "this" is tricky for beginners.
0

I think it would benefit you to provide a few concrete examples. It's difficult to help as well as would be possible without such. However, I can provide examples of how one constructs a regular expression from variables.

Let's assume your variable from which you wish to construct an "on-the-fly" regular expression matching pattern is

const someVar = 'LostInJavaScript';

Use of the Regular Expression constructor, as you seem to be using, is the necessary approach to take here. Inside the constructor, I tend to favor using ES2015-style string templates for the first argument passed it:

const regPattern = new RegExp(`${someVar}`, 'g');
// => /LostInJavaScript/g

Apply the relevant/appropriate String method (e.g., String#match, String#search, RegExp#test, RegExp#exec) passing it this dynamically created regular expression.

const strInput = 'RegularExpressionsHaveMeLostInJavaScript...',
      matchedSubStr = strInput.match(regPattern);
// => ["LostInJavaScript"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.