0

I need a javascript regex pattern which can break down a given string into substrings. These substrings will be separated by the comma in the master string, and I want the regex to detect that comma "," and separate or break down the whole master string on the basis of the comma. And make an array of those substrings. If it is possible by any javascript built in method, then it would be appreciated more, Thanks.

Example :

var masterString = "3 Bedrooms, 5 Bathrooms, 10 Dinning rooms, Garden, Kitchen";

var subStrArr = ["3 Bedrooms", "5 Bathrooms", "10 Dinning rooms", "Garden", "Kitchen"];
Toan Tran
  • 1,937
  • 1
  • 24
  • 37
Kode Gylaxy
  • 71
  • 1
  • 8

2 Answers2

3

Use split instead of a regex:

masterString.split(",")

return:

["3 Bedrooms", "5 Bathrooms", "10 Dinning rooms", "Garden", "Kitchen"];

split works also with string longer than one:

masterString.split(", ")

remove the whitespace after the comma.

Antonio Ganci
  • 515
  • 5
  • 16
0

You may use split()

Eg var masterStringArray = masterString.split(",")

It will return an array of elements splitted by string