简体   繁体   English

返回字符串中每个单词的第一个字符

[英]Return First Character of each word in a string

I have a string with Names and I want to convert them to Initials.我有一个带有名称的字符串,我想将它们转换为缩写。 Eg:例如:

  • John White -> JW约翰·怀特 -> JW
  • John P White -> JPW约翰·P·怀特 -> JPW

I am looking for a suggesting on how best to do this.我正在寻找有关如何最好地执行此操作的建议。 I have researched using split() and looked here http://www.w3schools.com/jsref/jsref_split.asp but could not find a good example of how to target and return first letter when I don't know the char to look for.我研究过使用split()并在这里查看http://www.w3schools.com/jsref/jsref_split.asp但是找不到一个很好的例子来说明当我不知道要查找的字符时如何定位和返回第一个字母为了。

My string looks like this我的字符串看起来像这样

<a href="#">John White</a>

and I want to change to this我想改成这个

<a href="#">JW</a>

Any help?有什么帮助吗? Thanks谢谢

I would suggest you to use RegExp , Here's an Example我建议你使用RegExp ,这是一个例子

 var myStr = "John P White"; var matches = myStr.match(/\\b(\\w)/g); console.log(matches.join(''));

\\b assert position at a word boundary (^\\w|\\w$|\\W\\w|\\w\\W) \\b在单词边界处断言位置(^\\w|\\w$|\\W\\w|\\w\\W)

1st Capturing Group ( \\w )第一个捕获组 ( \\w )

\\w matches any word character (equal to [a-zA-Z0-9_] ) \\w匹配任何单词字符(等于[a-zA-Z0-9_]

Global pattern flags全局模式标志

g modifier: global. g修饰符:全局。 All matches (don't return after first match)所有比赛(第一场比赛后不返回)

For better explanation visit here为了更好的解释,请访问这里

var name = "John P White";

function initials(value){
    var result = "";
    var tokens = value.split(/\s/);
    for(var i =0; i < tokens.length; i++){
      result += tokens[i].substring(0,1).toUpperCase();
    }
    return result;
}

initials(name);

You can do:你可以做:

var words = $('a').text().split(' ');
var text = '';
$.each(words, function () {
    text +=  this.substring(0, 1);
});
$('a').text(text);

Fiddle Demo小提琴演示

You can do the following:您可以执行以下操作:

var name = "John P White",
    data = name.split(' '), output = "";

for ( var i = 0; i < data.length; i++) {
    output += data[i].substring(0,1);
}

alert(output);

Demo演示

With delimiter as space you can split the names and put it into an Array.使用分隔符作为space您可以拆分名称并将其放入数组中。 And then you can take the first index of each element inside the Array.然后你可以取数组中每个元素的第一个索引。

You can use a regex to match anything that's not an uppercase letter and then simply remove it.您可以使用正则表达式来匹配不是大写字母的任何内容,然后简单地将其删除。

<a href="#">John White</a>
<a href="#">John P Brown</a>

$('a').each(function(){
    var initials = $(this).text().replace(/[^A-Z]/g, "");
    $(this).text(initials);
});

fiddle小提琴

Please find below the answer请在下面找到答案

 let example_one='John White'; example_one=example_one.split(' ').map(word=>word[0]).join(''); console.log(example_one); let example_two='John P White'; example_two=example_two.split(' ').map(word=>word[0]).join(''); console.log(example_two);

暂无
暂无

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

相关问题 JavaScript:如何返回字符串中每个单词的首字母 - JavaScript: how to return the first letter of each word in a string Javascript使用循环返回字符串中每个单词的第一个字母的位置 - Javascript Return the location of the first letter of each word in a string using a loop 给定一个字符串,将每个单词的第一个字母移动到每个单词的末尾,然后在每个单词的末尾添加“ay”并返回一个新字符串 - JavaScript - Given a String, Move The First Letter of Each Word to The End of Each Word, Then Add "ay" To The End of Each Word and Return a New String - JavaScript 检查字符串中单词的第一个字符以@开头 - Check first character of a word in a string begins with @ 如何使每个句子的第一个单词第一个字符大写 - How to make first word first character capital of each sentence 通过将每个单词的每个第一个字母大写并以小写形式保留来返回字符串 - Return String by capitalizing every first letter of the each word and rest in lower case 如何将缩写词和字符串中的每个首字母大写? - How to Capitalize Abbreviations and each first word in string? 在 JavaScript 中获取字符串中每个单词的首字母 - Get first letter of each word in a string, in JavaScript 将字符串中的每个第一个字符转换为大写 - Transformation of each first character in a string to uppercase Javascript 删除字符串每行的第一个字符 - Javascript Delete First Character on Each Line of String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM