0

Here I have text something like this:

  1. const string = 'Welcome;;to::';
  2. const string ='Welcome;;';

I want to split this string with ;; and ::;

Expected result: ['Welcome', ';;', 'to', '::']

There will be chance that number of pattern will be increased. So, is this possible ?

Note: There is no space between words.

7
  • 1
    Split by non-word characters and use a capturing group so it's included in the resulting array Commented May 30, 2022 at 13:56
  • See also: stackoverflow.com/questions/650022/… Commented May 30, 2022 at 13:56
  • result = string.split(";;").join(" ;; ").split("::").join(" ::").split(" "); Commented May 30, 2022 at 14:02
  • 3
    or result = string.replace(";;", " ;; ").replace("::", " ::").split(" "); You basically need to insert spaces around the delimiters and perform a final split to remove them. Commented May 30, 2022 at 14:04
  • 2
    if there's always no spaces, you would simply pass each delimeter to replace("xx", " xx ") to insert the spaces before the final split on " ". If the string ends with a delimiter, as your example is, you'll have to only insert the space behind it (or remove the resulting element at the end of the array afterwards). Commented May 30, 2022 at 15:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.