正则表达式或jquery在特定字符后拆分字符串

时间:2022-06-01 20:28:20

I have a string like this:

我有一个像这样的字符串:

Franciscan St. Francis Health - Indianapolis

方济各会圣弗朗西斯健康 - 印第安纳波利斯

I need to extract everything after '-' including the dash itself and output it in the second line..How do I extract everything before '-'?? Regex or jquery?

我需要在' - '之后提取所有内容,包括破折号本身并在第二行输出它。如何在' - '之前提取所有内容?正则表达式还是jquery?

The string infront of '-' will be dynamic and could have varying number of letters...

' - '前面的字符串将是动态的,可能有不同数量的字母......

3 个解决方案

#1


10  

Neither. I would just use the native .split() function for strings:

都不是。我只想使用字符串的原生.split()函数:

var myString = 'Franciscan St. Francis Health - Indianapolis';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis']

Once you've crated the stringArray variable, you can output the original string's pieces in whatever way you want, for example:

一旦你创建了stringArray变量,就可以以任何你想要的方式输出原始字符串的片段,例如:

alert('-' + stringArray[1]); //alerts "- Indianapolis"

Edit
To address a commenter's follow-up question: "What if the string after the hyphen has another hyphen in it"?

编辑为了解决评论者的后续问题:“如果连字符后面的字符串中有另一个连字符怎么办?”

In that case, you could do something like this:

在这种情况下,你可以这样做:

var myString = 'Franciscan St. Francis Health - Indianapolis - IN';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis ', ' IN']

alert('-' + stringArray.slice(1).join('-')); //alerts "- Indianapolis - IN"

Both .slice() and .join() are native Array methods in JS, and join() is the opposite of the .split() method used earlier.

.slice()和.join()都是JS中的本机Array方法,join()与前面使用的.split()方法相反。

#2


2  

Regex or jquery?

正则表达式还是jquery?

False dichotomy. Use String.splitMDN

假二分法。使用String.splitMDN

var tokens = 'Franciscan St. Francis Health - Indianapolis'.split('-');
var s = tokens.slice(1).join('-'); // account for '-'s in city name
alert('-' + s);

#3


1  

Probably no need for regex or jquery. This should do it:

可能不需要正则表达式或jquery。这应该这样做:

var arr = 'Franciscan St. Francis Health - Wilkes-Barre'.split('-');
var firstLine = arr[0]
var secondLine = arr.slice(1).join('-');

Ideally, your data would be stored in two separate fields, so you don't have to worry about splitting strings for display.

理想情况下,您的数据将存储在两个单独的字段中,因此您不必担心拆分字符串以进行显示。

#1


10  

Neither. I would just use the native .split() function for strings:

都不是。我只想使用字符串的原生.split()函数:

var myString = 'Franciscan St. Francis Health - Indianapolis';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis']

Once you've crated the stringArray variable, you can output the original string's pieces in whatever way you want, for example:

一旦你创建了stringArray变量,就可以以任何你想要的方式输出原始字符串的片段,例如:

alert('-' + stringArray[1]); //alerts "- Indianapolis"

Edit
To address a commenter's follow-up question: "What if the string after the hyphen has another hyphen in it"?

编辑为了解决评论者的后续问题:“如果连字符后面的字符串中有另一个连字符怎么办?”

In that case, you could do something like this:

在这种情况下,你可以这样做:

var myString = 'Franciscan St. Francis Health - Indianapolis - IN';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis ', ' IN']

alert('-' + stringArray.slice(1).join('-')); //alerts "- Indianapolis - IN"

Both .slice() and .join() are native Array methods in JS, and join() is the opposite of the .split() method used earlier.

.slice()和.join()都是JS中的本机Array方法,join()与前面使用的.split()方法相反。

#2


2  

Regex or jquery?

正则表达式还是jquery?

False dichotomy. Use String.splitMDN

假二分法。使用String.splitMDN

var tokens = 'Franciscan St. Francis Health - Indianapolis'.split('-');
var s = tokens.slice(1).join('-'); // account for '-'s in city name
alert('-' + s);

#3


1  

Probably no need for regex or jquery. This should do it:

可能不需要正则表达式或jquery。这应该这样做:

var arr = 'Franciscan St. Francis Health - Wilkes-Barre'.split('-');
var firstLine = arr[0]
var secondLine = arr.slice(1).join('-');

Ideally, your data would be stored in two separate fields, so you don't have to worry about splitting strings for display.

理想情况下,您的数据将存储在两个单独的字段中,因此您不必担心拆分字符串以进行显示。