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>

3
  • Possible duplicate of How can I concatenate regex literals in JavaScript? Commented Oct 4, 2017 at 5: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 stackoverflow.com/help/mcve Commented Oct 4, 2017 at 5: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. Commented Oct 4, 2017 at 6:07

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I have used the constructor as you suggested. I will edit my post to include a snippet as a better explanation.
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');
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.
0

Check out below solution :

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

2 Comments

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.
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.

Your Answer

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