Avoid escape sequence and split the string in Javascript

I have a requirement to split the below string and get the values. Have tried with the JS string split method but it is not working for backslash split(). Please help me. Thanks in advance.

Input string = "11,22;0:0/0\0}0#0&"

Output:
, => 11,
; => 22
: => 0
/ => 0
\ => 0
} => 0
# => 0
& => 0

Upvotes: 1

Views: 189

Answers (2)

keidakida
keidakida

Reputation: 751

I made a modification with reference to a lot of reading. Hope you find this useful. The '0' with an escape character is also printed in this solution. Please check it.

let string = "26,67;4:79/9\0}0&";

    string = encodeURI(string);
    string = string.replace("%0", "&");
    string = decodeURI(string);
    numbers = string.split(/,|;|:|\/|\\|}|&/);
    finalList = [];
    for (let i = 0; i < numbers.length; i++) {
        if (numbers[i] != "")
            finalList.push(parseInt(numbers[i]));
    }
    console.log(finalList);

Upvotes: 1

Sai
Sai

Reputation: 2668

It is working fine for me. Please try the below code.

let string = "26,67;4:79/9\0}0&";
let arr = string.split(/,|;|:|\/|\\|}|&/);     // ["26", "67", "4", "79", "9 ", "0", ""]

Thank you...

Upvotes: 0

Related Questions