0

I have a task to split the string. First, there are a few operations to perform

let currSynonyms = "['rear neck and choke', 'multiple fractures, multiple hairline injuries', 'leukemia']"
currSynonyms = currSynonyms.replace('[', '');
currSynonyms = currSynonyms.replace(']', '');
console.log(currSynonyms);
Output is this string:
'rear neck and choke', 'multiple fractures, multiple hairline injuries', 'leukemia'

Now I have to split this so that the 2nd element is not separated with the comma present there. So, my output should be an array like this:

['rear neck and choke', 'multiple fractures, multiple hairline injuries', 'leukemia']

If I do currSynonyms.split(',') I get:

[
  "'rear neck and choke'",
  " 'multiple fractures",
  " multiple hairline injuries'",
  " 'leukemia'"
]

If you do currSynonyms.length you'll get 4 but this is not what I want. I know I have to somehow use Regular expressions nut I can't figure out how?

3
  • 1
    How certain are you about the input string format? Are you in control of it? Slightly silly answer: Remove the first and last two characters, then split by ', '. Is the problem more complex? Commented Aug 29, 2020 at 7:12
  • I'm certain enough. Not in so much control I have to operate with this input itself Commented Aug 29, 2020 at 7:13
  • @TheMaster Yeah, this is what I'm also looking for. A regular expression can solve this but I don't know how? Commented Aug 29, 2020 at 7:15

4 Answers 4

1
let currSynonyms = "['rear neck and choke', 'multiple fractures, multiple hairline injuries', 'leukemia']"

const splittedAsArray = JSON.parse(currSynonyms.replace(/'/g,"\""))
const result = splittedAsArray.join(",").split(",").map(r=>r.trim())
            


console.log(result);

This should result in

[ 'rear neck and choke',
  'multiple fractures',
  'multiple hairline injuries',
  'leukemia' ]
Sign up to request clarification or add additional context in comments.

Comments

1

Replace ' with " and JSON.parse it:

let arr = "['abc','xyz','shell's, disease']";
const out = JSON.parse(arr.replace(/'(?=,|])|(?<=,|\[)'/g,'"'))
console.info(out)

  • updated regular expression to catch ' inside strings

Comments

0

JSON.parse would create a problem if the string is like this:

let arr = "['abc','xyz','shell's, disease']";

Here, the 3rd element has a single quote in it which gives an error when you try to parse it. So, I came up with a better solution:

let arr = "['abc','xyz','shell's, disease']";
arr = arr.replace('[', '')
arr = arr.replace(']', '')
arr = arr.split(/',/);
console.log(arr.length);
console.log(arr);

Output:
3
[ "'abc", "'xyz", "'shell's, disease'" ]

Comments

0

Since from the given example one also might need to take single quote characters used as apostrophs into account, I suggest a regex based approach like the following one ...

  1. remove leading opening bracket and single quote.
  2. remove trailing single quote and closing bracket.
  3. split at following sequence ...
    • single quote, optional white space sequence (OWSS), comma, OWSS, single quote.

... example ...

const sample = "['rear neck and choke', 'multiple fractures, multiple hairline injuries', 'leukemia','shell's disease']";

/**
 *  1. remove leading opening bracket and single quote.
 *  2. remove trailing single quote and closing bracket.
 *  3. split at following sequence ...
 *     - single quote, optional white space sequence (OWSS),
 *       comma, OWSS, single quote.
 */
console.log(`"${ sample }"`);

console.log(sample
  .replace(/^\['/, '')
  .replace(/'\]$/, '')
  .split(/'\s*,\s*'/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.