I want to test various regexp against a specific piece of text, so I wrote a function that creates a new regexp from user input. It works fine, except that if I try to replace a given string with a new line (i.e. "\n"), I get the backslash and the "n" instead of a new line. My function looks like this:
function doIt() {
var wTA = document.getElementById('workingTextArea');
var workStr = wTA.value;
var iA = document.getElementById('inputArea');
var fA = document.getElementById('flagArea');
var rA = document.getElementById('replaceArea');
var formatStr = iA.value;
var flagStr = fA.value;
replaceStr = rA.value;
if (formatStr.length > 0) {
var rE = new RegExp(formatStr,[flagStr]);
workStr=workStr.replace(rE,replaceStr);
wTA.value = workStr;
} else {alert("No format string entered."); }
}
The problem occurs when replaceStr = "\n". I though I could deal with this by setting replaceStr to "\\n" but that just gets me two backslashes and then the "n".
What am I doing wrong here?
Thanks,
JAs
function doIt() {
var wTA = document.getElementById('workingTextArea');
var workStr = wTA.value;
var iA = document.getElementById('inputArea');
var fA = document.getElementById('flagArea');
var rA = document.getElementById('replaceArea');
var formatStr = iA.value;
var flagStr = fA.value;
replaceStr = rA.value;
if (formatStr.length > 0) {
var rE = new RegExp(formatStr,[flagStr]);
workStr=workStr.replace(rE,replaceStr);
wTA.value = workStr;
} else {alert("No format string entered."); }
}
The problem occurs when replaceStr = "\n". I though I could deal with this by setting replaceStr to "\\n" but that just gets me two backslashes and then the "n".
What am I doing wrong here?
Thanks,
JAs