-2

so I am still learning Javascript, so I know this is a basic questions, and I'd really like to learn what I'm missing. I have an array of variables, and I need a function that removes special characters, and returns the result as an array.

Here's my code:

    var myArray = [what_hap, desc_injury];
    function ds (string) {
    string.replace(/[\\]/g, ' ')
    string.replace(/[\"]/g, ' ')
    string.replace(/[\/]/g, '-')
    string.replace(/[\b]/g, ' ')
    string.replace(/[\f]/g, ' ')
    string.replace(/[\n]/g, ',')
    string.replace(/[\r]/g, ' ')
    string.replace(/[\t]/g, ' ');
    return string;
}
ds (myArray);

I know that's not going to work, so I'm just trying to learn the simplest and cleanest way to output:

[whatHap: TEXTw/oSpecialCharacters, descInj: TEXTw/oSpecialCharacters]

Anyone willing to guide a noobie? Thanks! :)

tommycopeland
  • 59
  • 1
  • 11
  • 3
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – 31piy Jul 19 '18 at 15:01
  • https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript – Hyyan Abo Fakher Jul 19 '18 at 15:02
  • 1
    Please provice an example input/output pair. Posting desired output without the related input doesn't help much. Also, elaborate a bit on what you mean with "special characters". If you meant "characters that have a special escape sequence in js", i don't understand why "/" is included, yet e.g. "\0" is missing. My other guesses fail for other reasons, i cannot reconstruct what you mean. – ASDFGerte Jul 19 '18 at 15:15

2 Answers2

0

The comments on the question are correct, you need to specify what you are asking a little better but I will try and give you some guidance from what I assume about your intended result.

One important thing to note which would fix the function you already have is that string.replace() will not change the string itself, it returns a new string with the replacements as you can see in the documentation. to do many replacements you need to do string = string.replace('a', '-')

On to a solution for the whole array. There are a couple ways to process an array in javascript: for loop, Array.forEach(), or Array.map(). I urge you to read the documentation of each and look up examples on your own to understand each and where they are most useful.

Since you want to replace everything in your array I suggest using .map() or .foreach() since these will loop through the whole array for you without you having to keep track of the index yourself. Below are examples of using each to implement what I think you are going for.

Map

function removeSpecial(str) {
    // replace all these character with ' '
    // \ " \b \f \r \t
    str = str.replace(/[\\"\b\f\r\t]/g, ' ');
    // replace / with -
    str = str.replace(/\//g, '-');
    // replace \n with ,
    str = str.replace(/\n/g, ',');
    return str;
}
let myArray = ["string\\other", "test/path"];
let withoutSpecial = myArray.map(removeSpecial); // ["string other", "test-path"]

forEach

function removeSpecial(myArray) {
    let withoutSpecial = [];
    myArray.forEach(function(str) {
        str = str.replace(/[\\"\b\f\r\t]/g, ' ');
        // replace / with -
        str = str.replace(/\//g, '-');
        // replace \n with ,
        str = str.replace(/\n/g, ',');
        withoutSpecial.push(str)
    });
    return withoutSpecial;
}

let myArray = ["string\\other", "test/path"];
let withoutSpecial = removeSpecial(myArray);  // ["string other", "test-path"]

The internalals of each function's can be whatever replacements you need it to be or you could replace them with the function you already have. Map is stronger in this situation because it will replace the values in the array, it's used to map the existing values to new corresponding values one to one for every element. On the other hand the forEach solution requires you to create and add elements to a new array, this is better for when you need to do something outside the array itself for every element in the array.

PS. you should check out https://regex101.com/ for help building regular expressions if you want a more complex replacements but you dont really need them for this situation

jjspace
  • 178
  • 2
  • 11
  • Thanks for taking the time to write this. Very helpful, especially on the string action. And I've read about forEach, but I just wasn't sure how to implement it here. Thank you! – tommycopeland Jul 20 '18 at 18:37
0

I realize that the way I wrote my goal isn't exactly clear. I think what I should have said was that given several text strings, I want to strip out some specific characters (quotes, for example), and then output each of those into an array that can be accessed. I have read about arrays, it's just been my experience in learning JS that reading code and actually doing code are two very different things.

So I appreciate the references to documentation, what I really needed to see was a real life example code.

I ended up finding a solution that works:

function escapeData(data) {
        return data
          .replace(/\r/g, "");
    }

        var result = {};
        result.what_hap_escaped = escapeData($what_hap);
        result.desc_injury_escaped = escapeData($desc_injury);

        result;

I appreciate everyone's time, and hope I didn't annoy you guys too much with my poorly constructed question :)

tommycopeland
  • 59
  • 1
  • 11