2

I have a string that I turn into an array using split (""). For example:

let str = "add 2017-04-25 2 USD Jogurt"
str.split(" ");
["add", "2017-04-25", "2", "USD", "Jogurt"]

But how can I add two words to one element to make it like this?

add 2017-04-25 3 EUR “French fries”
["add", "2017-04-25", "3", "EUR", "French fries"]

I know that it can be use regular expression but how i don't know. maybe split by "" in this command: add 2017-04-25 3 EUR “French fries”

Volodumr
  • 173
  • 1
  • 12

4 Answers4

0

If the provided expression template is mostly static and always looks like "ACTION DATE QUANTITY CURRENCY_IN_ISO_4217_FORMAT ITEM_NAME", then regex could be handy:

"add 2017-04-25 2 USD Jogurt".replace(/([A-Z]{3}\s)(.*)$/, '$1“French fries”')
0

You can give this a try...

let str = "add ;2017-04-25 ;3 ;EUR ;French fries";
let reg = /\s*(?:;|$)\s*/;
let strList = str.split(reg);
console.log(strList[4]);
Alucard777
  • 50
  • 7
0

if you wish to use a custom made function instead of regex than here is the code did not test for performance against regex.

input string = 'add 2017-04-25 3 EUR "French crispy fries" and "tasty burger"'

output array = ["add", "2017-04-25", "3", "EUR", "French crispy fries", "and", "tasty burger"]

    function myFunction() {
  var str = 'add 2017-04-25 3 EUR "French crispy fries" and "tasty burger"';
  var result = str.split(" ");
  var resultString = [];
  var quotedString = [];
  var ignoreSplit = '"';
  push = 0;
  for(var i=0;i<result.length;i++)
  {
    //start pushing if you found double quotes on starting
    if(result[i].indexOf(ignoreSplit)==0)
    {
            push = 1;
            //replace double quotes to nothing and push
            quotedString.push(result[i].replace(ignoreSplit,''));
    }
    //push if double quotes found in end
    else if(result[i].indexOf(ignoreSplit)>0)
    {
        //stop the push in between
        push = 0;
        quotedString.push(result[i].replace(ignoreSplit,''));
        //push the quoted string in main result string
        resultString.push(quotedString.join(' '));
        //empty quoted string to be used again
        quotedString = [];
    }
    //push if between the double quotes area
    else if(push == 1)
    {
        quotedString.push(result[i]);   
    }
    //push in the main result string normal case
    else
    {
        resultString.push(result[i]);
    }
  }     
   console.log('final ',resultString);
}
ash1102
  • 409
  • 4
  • 20
0

Instead of using split, you might also use match to match either from the opening till the closing double quote or match 1+ non whitespace chars using an alternation.

Use the /g global flag to match all occurrences.

/(?<=“)[^“”]*(?=”)|[^\s“”]+/g

Explanation

  • (?<=“) Positive lookbehind, assert what is direcly to the left is
  • [^“”]* Match 0+ chars other than or using a negated character class
  • (?=”) Positive lookahead, assert what is directly to the right is
  • | Or
  • [^\s“”]+ Match 0+ chars other than or or a whitespace char

Regex demo

[
  "add 2017-04-25 2 USD Jogurt",
  "add 2017-04-25 3 EUR “French fries”"
].forEach(s => console.log(s.match(/(?<=“)[^“”]*(?=”)|[^\s“”]+/g)));
The fourth bird
  • 154,723
  • 16
  • 55
  • 70