0

The goal is to replace digits 0-9 as well as a variable value from a text input in a form, with a substitute value of x if a match is found, as user types, using javascript.

Why does this not work?

regexExpression = "([0-9]" + companyvalue + ")";

Each case works when coded independently but does not work when concatenated.

var regexExpression = ("[0-9]"); Works as expected and substitutes x for numeric value.

var regexExpression = "("companyvalue")"; Works as expected and substitutes x for the variable text input value.

function clean(e) {
 var companyvalue = document.getElementById("company").value;
 var textfield = document.getElementById(e);
 var regexExpression = ('([0-9]' + companyvalue + ')');
 var regex = new RegExp(regexExpression,"g","i");
  if(textfield.value.search(regex) > -1) {
  document.getElementById('status').innerHTML = "Telephone numbers and company name<br>are automatically redacted.";
 }
 textfield.value = textfield.value.replace(regex, "x");
 //alert (companyvalue);
 }
<input id="company" style="border-color:#F8980F;" class="medium required" type="text" name="company" />

<textarea id="description" onkeyup="clean('description')" onkeydown="clean('description')" class="medium" cols="25" rows="8" name="description" placeholder="Write a short summary." /></textarea>
<div id="status"></div>
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Possible duplicate of [How can I concatenate regex literals in JavaScript?](https://stackoverflow.com/questions/185510/how-can-i-concatenate-regex-literals-in-javascript) – Dream_Cap Oct 04 '17 at 05:29
  • 1
    Are the words separated by a space at input string? _"Each case works when coded independently but does not work when concantenated."_ Can you create a stacksnippets to demonstrate? See https://stackoverflow.com/help/mcve – guest271314 Oct 04 '17 at 05:29
  • Input string is a single word with no whitespace but you raise an interesting point as there may be cases where more than one word is used. I will edit my question to include a snippet. –  Oct 04 '17 at 06:07

2 Answers2

3

Check out the documentation on RegExp for an in depth explanation.

But basically, you can create a regular expression using literal notation:

/([0-9])/

Or using a RegExp constructor:

new RegExp('([0-9])');

Given that you're wanting to dynamically build your pattern, you need to use the constructor:

var re = new RegExp('([0-9]' + companyvalue + ')');

Edit: Check out this example: https://jsbin.com/zekovuqemi/edit?js,output

fubar
  • 16,918
  • 4
  • 37
  • 43
  • I have used the constructor as you suggested. I will edit my post to include a snippet as a better explanation. –  Oct 04 '17 at 06:09
  • The example you provided works perfectly. Appreciated. Please can you explain the purpose of the pipe operator in the following new RegExp('([0-9])|(' + company.value + ')', 'gi'); –  Oct 04 '17 at 10:37
  • The `|` is an OR. You don't want to match a number followed by the company name, you want to match and replace numbers OR the company name. – fubar Oct 04 '17 at 10:52
0

Check out below solution :

var pattern1 = '[0-9]'; var regex = new RegExp(pattern1 + companyvalue, "g");

sgajera
  • 146
  • 1
  • 9
  • Unfortunately the result is the same as mine when using your suggestion. The numeric value is correctly replaced but not the string value. If I write the result of the string to an alert or to document the value is correct so it is being correctly passed to the argument. –  Oct 04 '17 at 08:15
  • Here what I have used : `var pattern1 = '[0-9]'; var pattern2 = "abc"; var regex = new RegExp(pattern1 + pattern2, "g"); // at this point, the line above is the same as: var regex = /#abc#/g; var input = "Hello 8abc world 7abc again."; var output = input.replace(regex, "!!"); alert(output); // Hello !! world !! again.` And it is correctly replaced string with number. – sgajera Oct 04 '17 at 08:48