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
2 Answers
Reset to default 7Do 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
版权声明:本文标题:regex - Javascript Or JQuery Split Alpha Numeric String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741427318a2378150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论