使用rtrim的Javascript字符串计数
我有这样的字符串,例如:使用rtrim的Javascript字符串计数,javascript,regex,Javascript,Regex,我有这样的字符串,例如: var str = '1234567 ' + '89'; 7后的两个空字符可能更多 我有一个正则表达式 var text = str.replace(/(\r\n)|\r|\n|( )|( )|(\u00A0)/g, ' ') .replace(/<p>( |(<br>))<\/p>/gi, "\n")//weird TinyMCE new
var str = '1234567 ' +
'89';
7后的两个空字符可能更多
我有一个正则表达式
var text = str.replace(/(\r\n)|\r|\n|( )|( )|(\u00A0)/g, ' ')
.replace(/<p>( |(<br>))<\/p>/gi, "\n")//weird TinyMCE newline variants
.replace(/(\s*<\/p>)|(\s*<br[^<>]*>)|(\s*\n)/gi, "\n")
.replace(/<.[^<>]*>/g, '')//strip tags
.replace(/&[a-z]+;/g, 'a')//html_entity_decode
.replace(/^\s+|\s+$/g, '');//whitespace right trim
console.log(text.length) //
您可以使用删除每个空格。对于单个字符串,可以使用
使用数组保存字符串,然后就可以执行此操作
var arr = ['new ', 'text ', 'example ']; // array of strings
var text = arr.map(function(str){ // map to create a new array
return str.replace(/(\w+)\s+/, "$1"); // right trim
}).join(""); // join the array with empty string
console.log(text.length); // 14
谢谢,好的,但我只需要右边的修剪,左边的空字符应该保留。
var arr = ['new ', 'text ', 'example ']; // array of strings
var text = arr.map(function(str){ // map to create a new array
return str.replace(/(\w+)\s+/, "$1"); // right trim
}).join(""); // join the array with empty string
console.log(text.length); // 14