Skip to content Skip to sidebar Skip to footer

How To Construct A Regular Expression To Split This Text?

Hello everyone I am writing a script, the main idea is that I have a text with a fixed structure as follows: 'RBD|X|RBD|C|92173~GJHGWO.NAYE' 'SAMBORNSiPOSSSTHRa' 'RBD|X|RBD|C|92173

Solution 1:

Use a regular expression:

str = '"RBD|X|RBD|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa"';
str.split(/[\|"~\s]+/).filter(Boolean); // Output: ["RBD", "X", "RBD", "C", "92173", "GJHGWO.NAYE", "SAMBORNSiPOSSSTHRa"]

If you want to filter the period as well, add it in the square brackets of the regex with a backslash to escape it.

Solution 2:

Ok, let's begin... Get textarea value and trim it...

var splitWords = document.getElementById("texto").value.trim();

First of all you need to replace " symbol...

splitWords = splitWords.replace(/"/g, '');

Then split the lines because it's like table rows...

splitWords = splitWords.split('\n');

Then split each row by posible delimeters |, ~, ...

splitWords.forEach(function(rowValue,rowIndex) {
    splitWords[rowIndex] = rowValue.split(/[|~ ]/);
    console.log(rowIndex, splitWords[rowIndex]);
});

Console.log output will be:

0["RBD", "X", "RBD", "C", "92173", "GJHGWO.NAYE", "SAMBORNSiPOSSSTHRa"]1["RBD", "X", "RBD", "C", "92173", "GJHGX4.NAYE", "SAMBORNSiPOSSSTHRa"]2["RBD", "X", "RBD", "C", "92173", "GJHGX6.NAYE", "SAMBORNSiPOSSSTHRa"]3["RBD", "X", "RBD", "C", "92173", "GJHGX8.NAYE", "SAMBORNSiPOSSSTHRa"]4["RBD", "X", "RBD", "C", "92173", "GJHGXA.NAYE", "SAMBORNSiPOSSSTHRa"]5["RBD", "X", "RBD", "C", "92173", "GJHGXC.NAYE", "SAMBORNSiPOSSSTHRa"]

Then do whatever you want with 2-dimensional array splitWords...

Solution 3:

My proposal is:

<pid="demo"></p><textareacols=150rows=15id="texto">
"RBD|X|RBD|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGX4.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGX6.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGX8.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGXA.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X|RBD|C|92173~GJHGXC.NAYE" "SAMBORNSiPOSSSTHRa"
</textarea><script>var lines = document.getElementById("texto").value.split('\n');
    var splitWords  = lines.filter(function(v) { return v.length > 0})
                           .map(function(currentValue, index) {
        return currentValue.trim().replace(/^"([^"]+)"\s"([^"]+)"$/, '$1$2').split(/[|~]/);
    });
    console.log(JSON.stringify(splitWords, null, 4));
</script>

Post a Comment for "How To Construct A Regular Expression To Split This Text?"