3

I want to replace string in a paragraph where string may be combination of alphanumeric and slashes.

What i did:

var arrayFind = new Array('s\\if','t\\/');
var arrayReplace = new Array('If','q');
var arrayFindLength = arrayFind.length;

function replaceRightChar(str, parFind, parReplace){
    for (var i = 0; i < arrayFindLength; i++) {
        regex = new RegExp(parFind[i], "g");
        str = str.replace(regex, parReplace[i]);
    }
    alert(str);
}

var mainData="s\\if t\\/ h\\ s\\";
replaceRightChar(mainData, arrayFind, arrayReplace);

Error:

Uncaught SyntaxError: Invalid regular expression: /s/: \ at end of pattern

5
  • Where do reArrangeFind and reArrangeReplace come from? Are they supposed to be there or are they typo's for arrayFind and arrayReplace Commented Feb 26, 2016 at 10:33
  • Sorry it should be like this: replaceRightChar(mainData, arrayFind, arrayReplace); Commented Feb 26, 2016 at 10:36
  • The "Invalid regular expression" error message leads me to believe you need escape the "s" in mainData... My javascript regular expressions are not great but I suspect you need "\s". Commented Feb 26, 2016 at 10:45
  • @fiprojects I would be surprised if that works in any language. Commented Feb 26, 2016 at 11:33
  • 1) I have the following code (note it escapes the 's') myvar.toString().replace(/-|\s|\./g,"").replace(/([^A-Za-z])/g,""); and it works for me to get alpha input only 2) Without prejudice... comments feed the eyes but answers feed the mind. If you have a proposed solution, please share. Commented Feb 26, 2016 at 12:18

2 Answers 2

1

My tests do not end up with any error. You did have a problem with double escaping, though.

Array('s\\if','t\\/');

should be (if I got right what you want)

Array('s\\\\if','t\\\\/');

Working example: jsfiddle

Edit: I still think that the problem is the double escaping. I updated my fiddle to test all the possible combinations.

Essentially I doubled the arrayFind

var arrayFind1 = new Array('s\\if','t\\/');
var arrayFind2 = new Array('s\\\\if','t\\\\/');

and the mainData

var mainData1="s\if t\/ h\\ s\\";
var mainData2="s\\if t\\/ h\\ s\\";

and quadruplicated the call

replaceRightChar(mainData1, arrayFind1, arrayReplace);
replaceRightChar(mainData1, arrayFind2, arrayReplace);
replaceRightChar(mainData2, arrayFind1, arrayReplace);
replaceRightChar(mainData2, arrayFind2, arrayReplace);

I guess the first or the fourth call are what you need

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

3 Comments

I am looking for Array('s\\if','t\\/'); but it isn't replacing string.
I'm not completely sure what you mean. Can you edit your post adding the expected result?
okay, i have a textarea, where user can type any character. Lets suppose user typed strings like : s\if but i need to convert string 's\if' to 'If'
0

Just for sake of clarity I add another answer instead of editing my existing one. The point is that when the string comes from a textarea it is not really as if it came from var str=...

It is almost the same, but for escaping.

What works in that case is to use regular expressions defined with the /.../g notation and not with the new RegExp('...','g') notation or to double escape things.

Here a working example. The code:

var arrayFindRE = new Array(/s\\if/g,/t\\\//g);
var arrayFindStr = new Array('s\\\\if','t\\\\/');
var arrayReplace = new Array('If','q');
var arrayFindLength = arrayFindRE.length;

function replaceRegExp(str){
    for (var i = 0; i < arrayFindLength; i++) {
        str = str.replace(arrayFindRE[i], arrayReplace[i]);
    }
    alert(str);
}

function replaceString(str){
    for (var i = 0; i < arrayFindLength; i++) {
        var re = new RegExp(arrayFindStr[i],'g');
        str = str.replace(re, arrayReplace[i]);
    }
    alert(str);
}

(here replaceRegExp and replaceString are called with the value of a textarea in the fiddle).

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.