-3

I have:

0001-2015

How I can split this string to 0001 and 2015?

brso05
  • 13,142
  • 2
  • 21
  • 40
MonkeyBusiness
  • 583
  • 1
  • 8
  • 23
  • you can use javascript's split() function with '-' as a delimiter. It will return you an array having '0001' and '2015' as its elements. – ankur May 22 '15 at 14:53
  • What language is being used? – npinti May 22 '15 at 14:53
  • possible duplicate of [How do I split a string, breaking at a particular character?](http://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – brodoll May 22 '15 at 14:54
  • Is there a particular reason you need to use regex to do this? To readers it seems like overkill to use regex... – Tony Hinkle May 22 '15 at 14:54
  • Since the title states "regex match" ... `var r = '0001-2015'.match(/\d+/g);` – hwnd May 22 '15 at 14:54
  • Can you please be more specific? Do you mean you want to split the string at the `-` character with regex? – Captain Man May 22 '15 at 14:54
  • 1
    @CaptainMan Why did you remove `javascript` and `jQuery` tags from question – Tushar May 22 '15 at 14:55
  • @Tushar at first glance the question seemed to have nothing to do with anything except for regex, it wasn't clear form the text. – Captain Man May 22 '15 at 14:58
  • @CaptainMan regex is implemented in many different languages we should know what language he is working in... – brso05 May 22 '15 at 14:59
  • 1
    @CaptainMan Yes! But the tag `javascript` will make it clear than confusing after removing it – Tushar May 22 '15 at 14:59
  • @Monkey likely because this question shows no research on your part, [this](http://stackoverflow.com/questions/9607846/regex-or-jquery-to-split-string-after-certain-character) is the first result in Google for "split string in javascript jquery using regex" – Captain Man May 22 '15 at 16:00

2 Answers2

5

Use split instead of regex. It will be much faster than regex

var str = '0001-2015';
var arr = str.split('-'); // ["0001", "2015"]
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

If you really want to use regular expressions - you can use /(\d+)/g regex , this means "capturing group having one or more digits". It will exactly extract both digital parts from your string.

But in current specification of your task using split looks pretty enough.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71