0

How can I split text in parts in javascript: I have

Ingrédients Purée de fruits (mangue 25%, banane*), purs jus de fruits (pomme*, fruits de la passion* 18%, orange), 100% des ingrédients agricoles issus de l'agriculture biologique.

I want ths split in : text[0]= Ingrédients Purée de fruits (mangue 25%, banane*) text[1]= purs jus de fruits (pomme*, fruits de la passion* 18%, orange) text[2]= 100% des ingrédients agricoles issus de l'agriculture biologique

So it is split with "," but"," is also used somewhere else!, and you have to skip that!!

2
  • 1
    Can you articulate what the logic is for when a split should happen? Commented Dec 4, 2018 at 5:54
  • I want to split the ingredients list from food products in parts of single ingredient, so I can say This ingredient has this characteristics, But some times ingredients are build with one word and sometimes a whole sentence is used for only one single inredient with percentages included where i'm not interested. Commented Dec 4, 2018 at 22:46

3 Answers 3

1

You can replace ), with some special character preserving ) at the end and then split() it:

var str = "Ingrédients Purée de fruits (mangue 25%, banane*), purs jus de fruits (pomme*, fruits de la passion* 18%, orange), 100% des ingrédients agricoles issus de l'agriculture biologique.";
var text = str.replace(/\),/g, ')&').split('&').map(x=>x.trim());
console.log(text);

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

Comments

0

Well, unless you have something specific to split the array in. it can't be done

For now i see that string can be split on "," if it does not occur inside the "()"

So here is the code for that

var str = "Ingrédients Purée de fruits (mangue 25%, banane*), purs jus de fruits (pomme*, fruits de la passion* 18%, orange), 100% des ingrédients agricoles issus de l'agriculture biologique.";
var regex = /,(?![^(]*\)) /;
var splitString = str.split(regex);
console.log(splitString);

Comments

0

One option would be to use .match instead, and alternate between (non-comma, non-parentheses characters) and (parentheses \( followed by non-) characters, followed by a )). This way, commas will only result in a new item in the array result if the comma is outside of parentheses:

const str = `Ingrédients Purée de fruits (mangue 25%, banane*), purs jus de fruits (pomme*, fruits de la passion* 18%, orange), 100% des ingrédients agricoles issus de l'agriculture biologique.`;

console.log(
  str.match(/(?:[^,(]+|\([^)]+\))+/g)
);

To additionally trim out the leading spaces:

const str = `Ingrédients Purée de fruits (mangue 25%, banane*), purs jus de fruits (pomme*, fruits de la passion* 18%, orange), 100% des ingrédients agricoles issus de l'agriculture biologique.`;

console.log(
  str.match(/(?:[^ ,(][^,(]+|\([^)]+\))+/g)
);

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.