Home > Software design >  Split a Javascript string
Split a Javascript string

Time:04-06

Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12

I want to split the above string as three separate arrays 1st array = [Monday Tuesday Wednesday 8-12 12-5]

2nd array = [SMS]

3rd array = [Thursday 8-12 ]

I tried using split function, but dint work! I appreciate your kind help. Thanks in advance

CodePudding user response:

You can try something like this as long as the bold part of the string are always the same:

var s = "Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12"
var arr = []

arr.push(s.split(" Unavailable Times: ")[1])
s = s.split(" Unavailable Times: ")[0]

arr.unshift(s.split(" Other type of contact: ")[1])
s = s.split(" Other type of contact: ")[0]

arr.unshift(s.split("Available Times: ")[1])

console.log(arr)

CodePudding user response:

Use split with regex

let str = 'Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12'
let replace = str.replace('Available Times: ', '');
let arr = replace.split(/ Other type of contact: | Unavailable Times: /)
console.log(arr);

CodePudding user response:

Does the OP mean ["Monday Tuesday Wednesday 8-12 12-5"] or ["Monday", "Tuesday", "Wednesday", "8-12", "12-5"] and likewise for the two remaining ones ?.. or ... does the OP even mean a single array with three string items like ... ["Monday Tuesday Wednesday 8-12 12-5", "SMS", "Thursday 8-12"]. The question arises since the OP did not provide a proper syntax of the expected result and three separate arrays each with just a single item does not make any sense.

In case of just extracting the information as three separated string values the following regex might be of help ...

/Available Times:\s*(?<available>.*?)\s Other type of contact:\s*(?<contact>.*?)\s Unavailable Times:\s*(?<unavailable>.*?)\s*$/

  1. With a simple String.prototype.match one would receive an array which holds the match itself followed by the results of the 3 capturing groups

  2. In order to just receive an array without the match one needs to slice the array.

  3. One also can make use of named capturing groups via RegExp.prototype.exec and some Destructuring Assignment

const sampleData =
  'Available Times: Monday Tuesday Wednesday 8-12 12-5 Other type of contact: SMS Unavailable Times: Thursday 8-12';

// see ... [https://regex101.com/r/jVedS4/1]
const regXGroups =
  /Available Times:\s*(?<available>.*?)\s Other type of contact:\s*(?<contact>.*?)\s Unavailable Times:\s*(?<unavailable>.*?)\s*$/;

console.log(
  sampleData.match(regXGroups)
);
console.log(
  sampleData.match(regXGroups).slice(1)
);

const {
  unavailable,
  available,
  contact,
} = regXGroups.exec(sampleData)?.groups ?? {};

console.log([available, contact, unavailable]);
.as-console-wrapper { min-height: 100%!important; top: 0; }

  • Related