这里是我的代码,其中包含我尝试做的详细信息:
var str = "Hello m|sss sss|mmm ss"
//Now i separate them by "|"
var str1 = str.split('|');
//Now i want to get the first word of every split-ed sting parts:
for (var i=0;i<codelines.length;i++)
{
//What to do here to get the first word of every spilt
}
那我该怎么办:\ 我想得到的是firstword [0]将给出“Hello”,firstword [1]将给出“sss”而firstword [2]将给出“mmm”
答案 0 :(得分:47)
再次用空格拆分:
var firstWords = [];
for (var i=0;i<codelines.length;i++)
{
var words = codelines[i].split(" ");
firstWords.push(words[0]);
}
或者使用String.prototype.substr()(可能更快):
var firstWords = [];
for (var i=0;i<codelines.length;i++)
{
var codeLine = codelines[i];
var firstWord = codeLine.substr(0, codeLine.indexOf(" "));
firstWords.push(firstWord);
}
答案 1 :(得分:36)
使用正则表达式
var totalWords = "foo love bar very much.";
var firstWord = totalWords.replace(/ .*/,'');
$('body').append(firstWord);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
&#13;
答案 2 :(得分:10)
我用这个:
takeWhile
答案 3 :(得分:4)
如何使用underscorejs
str = "There are so many places on earth that I want to go, i just dont have time. :("
firstWord = _.first( str.split(" ") )
答案 4 :(得分:3)
var str = "Hello m|sss sss|mmm ss"
//Now i separate them by "|"
var str1 = str.split('|');
//Now i want to get the first word of every split-ed sting parts:
for (var i=0;i<str1.length;i++)
{
//What to do here to get the first word :)
var firstWord = str1[i].split(' ')[0];
alert(firstWord);
}
答案 5 :(得分:3)
对先前答案的改进(处理多行或标签字符串):
String.prototype.firstWord = function(){return this.replace(/\s.*/,'')}
String.prototype.firstWord = function(){let sp=this.search(/\s/);return sp<0?this:this.substr(0,sp)}
或 没有正则表达式:
String.prototype.firstWord = function(){
let sps=[this.indexOf(' '),this.indexOf('\u000A'),this.indexOf('\u0009')].
filter((e)=>e!==-1);
return sps.length? this.substr(0,Math.min(...sps)) : this;
}
示例:
String.prototype.firstWord = function(){return this.replace(/\s.*/,'')}
console.log(`linebreak
example 1`.firstWord()); // -> linebreak
console.log('space example 2'.firstWord()); // -> singleline
console.log('tab example 3'.firstWord()); // -> tab
&#13;
答案 6 :(得分:3)
要获取字符串的第一个单词,您可以这样做:
let myStr = "Hello World"
let firstWord = myStr.split(" ")[0]
答案 7 :(得分:2)
此代码应该为您提供第一个单词,
var str = "Hello m|sss sss|mmm ss"
//Now i separate them by "|"
var str1 = str.split('|');
//Now i want to get the first word of every split-ed sting parts:
for (var i=0;i<str1.length;i++)
{
//What to do here to get the first word :(
var words = str1[i].split(" ");
console.log(words[0]);
}
答案 8 :(得分:1)
我很惊讶没有提到这种方法:"Some string".split(' ').shift()
直接回答问题:
let firstWords = []
let str = "Hello m|sss sss|mmm ss";
const codeLines = str.split("|");
for (var i = 0; i < codeLines.length; i++) {
const first = codeLines[i].split(' ').shift()
firstWords.push(first)
}
答案 9 :(得分:1)
const getFirstWord = string => {
const firstWord = [];
for (let i = 0; i < string.length; i += 1) {
if (string[i] === ' ') break;
firstWord.push(string[i]);
}
return firstWord.join('');
};
console.log(getFirstWord('Hello World'));
或简化它:
const getFirstWord = string => {
const words = string.split(' ');
return words[0];
};
console.log(getFirstWord('Hello World'));
答案 10 :(得分:0)
最简单的方法之一就是这样
var totalWords = "my name is rahul.";
var firstWord = totalWords.replace(/ .*/, '');
alert(firstWord);
console.log(firstWord);
答案 11 :(得分:0)
在现代JS中,这是简化的,您可以编写如下代码:
const firstWords = str =>
str .split (/\|/) .map (s => s .split (/\s+/) [0])
const str = "Hello m|sss sss|mmm ss"
console .log (firstWords (str))
我们首先在|
上分割字符串,然后在结果数组中的任何空格上分割每个字符串,仅保留第一个。