字符串匹配angularjs/javascript最多2个字符
我有一个函数,它匹配空格后的所有字母。我是说字符串匹配angularjs/javascript最多2个字符,javascript,angularjs,regex,Javascript,Angularjs,Regex,我有一个函数,它匹配空格后的所有字母。我是说 "this is my string" --> tims 只是这样做: $scope.getLetters = function(str) { var matches = str.match(/\b(\w)/g); var acronym = matches.join(''); return acronym; } 其中str当然是我从html传递的字符串
"this is my string" --> tims
只是这样做:
$scope.getLetters = function(str) {
var matches = str.match(/\b(\w)/g);
var acronym = matches.join('');
return acronym;
}
其中str
当然是我从html传递的字符串。它工作得很好,但我最多可以提取2个字符。所以如果字符串是
"this is my string"
我只想
ti
,而不是其他人。我该怎么做?在加入之前使用切片:
var str = "this is my string";
str.match(/\b\w/g).slice(0,2).join('')
//=> "ti"