Hunt
Hunt

Reputation: 8425

split string by hyphen in javascript

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 quote inside the string

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

[0] Average Sized 
[1] Mega Church!

Upvotes: 19

Views: 54561

Answers (4)

Ishara Sandun
Ishara Sandun

Reputation: 737

Easiest Method is

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

Upvotes: 3

Stephen
Stephen

Reputation: 18917

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.

Upvotes: 17

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

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

arr = str.split('-');

Upvotes: 8

Silagy
Silagy

Reputation: 3083

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

Upvotes: 29

Related Questions