0

I have two possible example strings:

'Software Sheffield'

and

'Software IN Sheffield'

and I want to split the string if it has the keyword 'IN' in the middle of it.

So for example 1:

var string1 = 'Software Sheffield';

var string2 = '';

and for example 2:

var string1 = 'Software';

var string2 = 'Sheffield';

Can anyone help me achieve this?

So far I have tried:

var string1 = string.split(/[ IN ]+/);

var string2 = string.split(/+[ IN ]/);
4

5 回答 5

2

只需使用字符串作为分隔符(带空格)

.split(' IN ');
于 2013-11-05T10:38:46.110 回答
1

/[ IN ]+/表示“单个字符[SPACE]///重复 1 到无穷大次” I,因此它也匹配“ ”。N[SPACE]NINININININIII NNNNIIINIIII

你可以简单地使用一个字符串split()

var splitter = string.split(' IN ');
var string1 = splitter[0];
var string2 = (splitter.length >= 2 ? splitter[1] : '');
于 2013-11-05T10:39:10.843 回答
0

您的正则表达式 - [ IN ] 表示它匹配 I 字母和 N 字母。简单地

string.split('IN');
于 2013-11-05T10:38:36.077 回答
0

为什么在这里使用 jQuery 函数?只需使用字符串

'Software Sheffield'.split(' IN ');
于 2013-11-05T10:41:11.163 回答
0

你为什么不试试

 var strings = "Software IN Sheffield".split(" IN "); // this will be returning an array

 var string1 = strings[0];

 var string2 = strings[1];

检查这个http://jsbin.com/iKeCUnA/1/

于 2013-11-05T10:44:12.033 回答