0

My code isvar String = "e-commerce software|mobile" And I want to split this string into e-commerce software mobile with only one split sentence. I am not very familiar with regex.

Thank you for your help.

codemonkey
  • 331
  • 1
  • 4
  • 16
  • Have you managed to split it on either a space OR a `|` using regex? – Mike Cluck May 19 '16 at 21:15
  • Hi, I mean split it on both space and a | – codemonkey May 19 '16 at 21:15
  • I know what you mean. I'm asking how far you have gotten on your own. Have you figured out how to split it on at least one of those? I don't want to just hand out code. I'd rather you understood how to come upon the answer yourself. – Mike Cluck May 19 '16 at 21:16
  • Oh, I know how to split with them either a space OR a | by using string.split() method in javascript – codemonkey May 19 '16 at 21:21
  • Here is a good website to help create regexs: https://regex101.com/ – Dan Weber May 19 '16 at 21:25
  • Maybe this? `str.split(/[|\s]+/g)` By the way, it might help you to sit down and really learn regular expressions in the long run. ;-) – Claudia May 20 '16 at 00:06
  • Possible duplicate of [How do I split a string with multiple separators in javascript?](http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – user3335966 May 20 '16 at 00:48

3 Answers3

1

var String = "e-commerce software|mobile";

console.log(String.split(/[\s]|[\|]/));
0

As you know, you can split by one separator doing this:

String.split('|');

To do the same thing using a regular expression, you could type this:

String.split(/\|/);

With regular expressions, you can combine single possible expressions using []. So to split on either a space or | you could do this:

String.split(/[ |\|]/);
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0

Also you can use

String.split(/[ |]/g)
Roomm
  • 905
  • 11
  • 23