最初の 100 語のみを削除し、文字列から残っているものを保持したい。
私が以下に持っているコードは正反対です:
var short_description = description.split(' ').slice(0,100).join(' ');
最初の 100 語のみを削除し、文字列から残っているものを保持したい。
私が以下に持っているコードは正反対です:
var short_description = description.split(' ').slice(0,100).join(' ');
最初の引数を削除します。
var short_description = description.split(' ').slice(100).join(' ');
を使用slice(x, y)
すると からx
までの要素が得られますy
が、 を使用すると から配列の末尾までのslice(x)
要素が得られます。x
(注: 説明が 100 語未満の場合、これは空の文字列を返します。)
正規表現を使用することもできます:
var short_description = description.replace(/^([^ ]+ ){100}/, '');
正規表現の説明は次のとおりです。
^ beginning of string
( start a group
[^ ] any character that is not a space
+ one or more times
then a space
) end the group. now the group contains a word and a space.
{100} 100 times
次に、それらの 100 語を何も置き換えません。(注: 説明が 100 語未満の場合、この正規表現は説明をそのまま返します。)
//hii i am getting result using this function
var inputString = "This is file placed on Desktop"
inputString = removeNWords(inputString, 2)
console.log(inputString);
function removeNWords(input,n) {
var newString = input.replace(/\s+/g,' ').trim();
var x = newString.split(" ")
return x.slice(n,x.length).join(" ")
}