开发者

split string by hyphen in javascript

开发者 https://www.devze.com 2023-02-10 02:05 出处:网络
I want to split following string into two parts using split function of javascript original string is \'Average Sized\' - \'Mega Church!\'(with single quotes)

I want to split following string into two parts using split function of javascript

original string is 'Average Sized' - 'Mega Church!' (with single quotes)

please mark that there is a single qu开发者_如何学JAVAote inside the string

and i want to split it by hyphen symbol so the result would be

[0] Average Sized 
[1] Mega Church!


var str = "Average Sized - Mega Church!";
var arr = str.split("-");


try this:

"Average Sized - Mega Church!".split(/\s*\-\s*/g)

edit:

if you mean the original string INCLUDES the single quotes, this should work:

"'Average Sized - Mega Church!'".replace(/^'|'$/g, "").split(/\s*\-\s*/g)

if you just meant that the string is defined with single quotes, the original will work.


var str = "Average Sized - Mega Church!";
var arr = [];

arr = str.split('-');


Easiest Method is

var arr = "'Average Sized'-'Mega Church!'".replace(/'/ig,"").split("-")
0

精彩评论

暂无评论...
验证码 换一张
取 消