admin管理员组

文章数量:1289508

Salaamun Alekum

I am trying to search JavaScript split alpha-numeric string functionality but I am not able to find it.

I want to have functionality like Split function in JQuery

Example:

"NewFolder333".AplhaNumericSplit();
["NewFolder","333"]///Result Splitted Array

Thank You

Salaamun Alekum

I am trying to search JavaScript split alpha-numeric string functionality but I am not able to find it.

I want to have functionality like Split function in JQuery

Example:

"NewFolder333".AplhaNumericSplit();
["NewFolder","333"]///Result Splitted Array

Thank You

Share Improve this question edited Jun 20, 2017 at 7:02 Ali Jamal asked Dec 17, 2015 at 6:19 Ali JamalAli Jamal 84011 silver badges19 bronze badges 4
  • Can you add all possible input strings – Tushar Commented Dec 17, 2015 at 6:22
  • Changed To split("NewFolder333"); – Ali Jamal Commented Dec 17, 2015 at 6:24
  • i dont think there is any direct methods in jquery or javscript for your purpose, i would suggest you to create simple manual parser for this – dreamweiver Commented Dec 17, 2015 at 6:24
  • 4 @AliJamal Please stop rolling back valid edits that improve your post. – Jon Clements Commented Dec 17, 2015 at 7:07
Add a ment  | 

2 Answers 2

Reset to default 7

Do matching instead of splitting..

string.match(/[a-z]+|\d+/ig)

tl;dr

<string>.match(/[^\d]+|\d+/g)

To split an Alpha Numeric String in order to obtain ["NewFolder" ,"333"] from "NewFolder333" or ["section", "4", "rubrique", "2"] from "section-4-rubrique-2" you can use a Regex as pattern of match().

  • String.prototype.match.

The RegExp pattern will be interested you is

/[^\d]+|\d+/g
  • // : Create a regex.
  • g : Allow your match function to obtain all matched part.
  • [^\d]+ : Obtain a string that contain possibly all char except numeric.
  • ´| : OR.
  • \d+ : Obtain all numeric char.

So in following code, result will contain ["NewFolder" ,"333"]

var result = "NewFolder333".match(/[^\d]+|\d+/g);
alert(result);


With a little modification, you will also be able to ignore , _ or -

  • [^-_ \d]+ : Obtain a string that contain possibly all char except numeric, , _ or -.

And following result will contain ["section", "4", "rubrique", "2"]

var result = "section-4-rubrique-2".match(/[^-_ \d]+|\d+/g);
alert(result);

or

var result = "section_4-rubrique_2".match(/[^-_ \d]+|\d+/g);
alert(result);

or

var result = "section-4 rubrique-2".match(/[^-_ \d]+|\d+/g);
alert(result);

本文标签: regexJavascript Or JQuery Split Alpha Numeric StringStack Overflow