javascript javascript在特殊字符处拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25266372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:16:16  来源:igfitidea点击:

javascript splitting a string at special character

javascriptstringsplitemail-validation

提问by Cycs

I am trying to "intelligently" pre-fill a form, I want to prefill the firstname and lastname inputs based on a user email address, so for example,

我正在尝试“智能地”预填表格,我想根据用户电子邮件地址预填名字和姓氏输入,例如,

[email protected] RETURNS Jon Doe
[email protected] RETURN Jon Doe
[email protected] RETURNS Jon Doe

[email protected]
返回Jon Doe [email protected] 返回Jon Doe [email protected] 返回
Jon Doe

I have managed to get the string before the @,

我已经设法在@,之前获得字符串

var email = letters.substr(0, letters.indexOf('@'));

But cant work out how to split() when the separator can be multiple values, I can do this,

但是当分隔符可以是多个值时无法弄清楚如何 split(),我可以这样做,

email.split("_")

but how can I split on other email address valid special characters?

但是如何拆分其他​​电子邮件地址有效的特殊字符?

回答by Alexander O'Mara

JavaScript's string splitmethodcan take a regex.

JavaScript 的字符串split方法可以采用正则表达式。

For example the following will split on ., -, and _.

例如下面将分为上.-_

"i-am_john.doe".split(/[.\-_]/)

Returning the following.

返回以下内容。

["i", "am", "john", "doe"]

回答by Guffa

You can use a regular expression for what you want to split on. You can for example split on anything that isn't a letter:

您可以对要拆分的内容使用正则表达式。例如,您可以拆分任何不是字母的内容:

var parts = email.split(/[^A-Za-z]/);

Demo: http://jsfiddle.net/Guffa/xt3Lb9e6/

演示:http: //jsfiddle.net/Guffa/xt3Lb9e6/

回答by Kara Brightwell

You can split a string using a regular expression. To match ., _or -, you can use a character class, for example [.\-_]. The syntax for regular expressions in JavaScript is /expression/, so your example would look like:

您可以使用正则表达式拆分字符串。要匹配._-,您可以使用字符类,例如[.\-_]。JavaScript 中正则表达式的语法是/expression/,因此您的示例如下所示:

email.split(/[\.\-_]/);

Note that the backslashes are to prevent .and -being interpreted as special characters. .is a special character class representing any character. In a character class, -can be used to specify ranges, such as [a-z].

请注意,反斜杠是防止.-被解释为特殊字符。 .是代表任何字符的特殊字符类。在字符类中,-可用于指定范围,例如[a-z].

If you require a dynamic list of characters to split on, you can build a regular expression using the RegExpconstructor. For example:

如果需要拆分的动态字符列表,可以使用RegExp构造函数构建正则表达式。例如:

var specialChars = ['.', '\-', '_'];
var specialRegex = new RegExp('[' + specialChars.join('') + ']');
email.split(specialRegex);

More information on regular expressions in JavaScript can be found on MDN.

更多关于 JavaScript中正则表达式的信息可以在MDN上找到。

回答by Kami

You are correct that you need to use the split function.

您是正确的,您需要使用split 功能

Split function works by taking an argument to split the string on. Multiple values can be split via regular expression. For you usage, try something like

Split 函数的工作原理是使用一个参数来拆分字符串。可以通过正则表达式拆分多个值。对于您的使用,请尝试类似

var re = /[\._\-]/;
var split = email.split(re, 2);

This should result in an array with two values, first/second name. The second argument is the number of elements returned.

这应该导致一个具有两个值的数组,第一个/第二个名称。第二个参数是返回的元素数。

回答by David Laberge

I created a jsFiddle to show how this could be done :

我创建了一个 jsFiddle 来展示如何做到这一点:

function printName(email){
    var name = email.split('@')[0];
    // source : http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript
    var returnVal = name.split(/[._-]/g);
    return returnVal;
}

http://jsfiddle.net/ts6nx9tt/1/

http://jsfiddle.net/ts6nx9tt/1/

回答by ymutlu

If you define your seperators, below code can return all alternatives for you.

如果您定义了分隔符,下面的代码可以为您返回所有选项。

var arr = ["_",".","-"];

var email = letters.substr(0, letters.indexOf('@'));
arr.map(function(val,index,rest){
   var r = email.split(val);
   if(r.length > 1){
     return r.join(' ');
   }
   return "";
}
);

回答by Zyga

You can use regex to do it, just provide a list of the characters in square brackets and escape if necessary.

您可以使用正则表达式来做到这一点,只需提供方括号中的字符列表,并在必要时转义。

email.split("[_-\.]");

email.split("[_-\.]");

Is that what you mean?

你是这个意思吗?

回答by furydevoid

Regular Expressions --

常用表达 -

email.split(/[_\.-]/)

This one matches (therefore splits at) any of (a character set, indicated by []) _, ., or -.

这个匹配(因此拆分)任何(字符集,由 [] 表示)_、. 或 -。

Here's a good resource for learning regular expressions: http://qntm.org/files/re/re.html

这是学习正则表达式的好资源:http: //qntm.org/files/re/re.html