简体   繁体   English

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

[英]javascript splitting a string at special character

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, 我试图“智能地”预填充表单,我想根据用户的电子邮件地址预填充名字和姓氏输入,例如,

jon.doe@email.com RETURNS Jon Doe jon.doe@email.com返回Jon Doe
jon_doe@email.com RETURN Jon Doe jon_doe@email.com返回Jon Doe
jon-doe@email.com RETURNS Jon Doe jon-doe@email.com返回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? 但是如何拆分其他​​电子邮件地址有效的特殊字符?

JavaScript's string split method can 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"]

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/

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 [az] . 在字符类中, -可用于指定范围,例如[az]

If you require a dynamic list of characters to split on, you can build a regular expression using the RegExp constructor. 如果需要分割动态字符列表,可以使用RegExp构造函数构建正则表达式。 For example: 例如:

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上找到。

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? 你是这个意思吗?

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

You are correct that you need to use the split function . 你是正确的,你需要使用拆分功能

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 为了您的使用,尝试类似的东西

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. 第二个参数是返回的元素数。

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/

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 "";
}
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM