抱歉我的英语很差。
在创建我的私人投资组合网站时,我找到了
textTest1();
textTest2();
function textTest1(){
var pText1 = $('p.textTest1').text();
var newString ='';
for(var i=0;i<pText1.length;i++){
console.log('total index of text1 is'+ i);
}
}
function textTest2(){
var pText2 = $('p.textTest2').text();
var newString2 ='';
for(var i=0; i<pText2.length;i++){
console.log('total index of text2 is' + i);
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="textTest1">text test</p>
<p class="textTest2">
text
test
</p>
&#13;
这两个p标签的长度不同。我明白为什么会发生
因为我和CSS有类似的问题。
如果文字很小,没问题,但如果文字需要更多
,那就太烦人了 超过两三行。将所有文本写在一行中可以解决问题但文本很长并且
里面有一些标签(例如:span,i)阅读
会很糟糕并在文本编辑器中找到。
有人有任何想法解决或避免这样的问题吗? (我的意思是
获得真实且正确的文本长度)
答案 0 :(得分:1)
主要问题是空白(空格和新线)。我们可以通过以下方式看到这一点:
var pText1 = $('p.textTest1').text();
var pText2 = $('p.textTest2').text();
console.log('"' + pText1 + '"', pText1.length);
console.log('"' + pText2 + '"', pText2.length);
//To fix this, one solution could be to strip all non-alphanumeric characters:
pText1 = pText1.replace(/\W+/g, '');
pText2 = pText2.replace(/\W+/g, '');
console.log('"' + pText1 + '"', pText1.length);
console.log('"' + pText2 + '"', pText2.length);
//Even better, let's create a function that just handles excess whitespace.
function cleanText(str) {
//Trim all whitespace at the start and end
str = str.trim();
//Replace all whitespace (new lines, excess spaces) with just one space
str = str.replace(/\s+/g, ' ');
return str;
}
console.log('cleanText():');
pText1 = cleanText($('p.textTest1').text());
pText2 = cleanText($('p.textTest2').text());
console.log('"' + pText1 + '"', pText1.length);
console.log('"' + pText2 + '"', pText2.length);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="textTest1">text test</p>
<p class="textTest2">
text
test
</p>
&#13;
每个空格,制表符和新行计为.length
计算的1个字符。
答案 1 :(得分:1)
.trim()
以删除前导和尾随空格。.replace()
删除单个空格的单词之间的空格,换行符和回车符。 此外,您不需要遍历字符串以获取其长度,只需使用.length
属性:
function getTrueLength(str){
// Trim off the leading and trailing spaces, then replace the space,
// returns and new lines in the middle with a single space
return str.text().trim().replace(/(\s+|\r+|\n+)/, " ").length;
}
console.log('total index of text1 is ' + getTrueLength($('p.textTest1')));
console.log('total index of text2 is ' + getTrueLength($('p.textTest2')));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="textTest1">text
test</p>
<p class="textTest2">
text
test
</p>
&#13;
答案 2 :(得分:0)
使用正则表达式删除新行和标签。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<p class="textTest1">text test</p>
<p class="textTest2">
text
test
</p>
<script>
textTest1();
textTest2();
function textTest1(){
var pText1 = $('p.textTest1').text();
var newString ='';
for(var i=0;i<pText1.length;i++){
console.log('total index of text1 is'+ i);
}
}
function textTest2(){
var pText2 = $('p.textTest2').text()
.replace(/\n/g, '')
.replace(/\t/g, ''); // and any other replace you want
console.log(pText2);
var newString2 ='';
for(var i=0; i<pText2.length;i++){
console.log('total index of text2 is' + i);
}
}
</script>
</body></html>
答案 3 :(得分:0)
使用:
<%= f.label :birthdate_cont, "Birth Date" %>
<%= f.text_field :birthdate_cont, class: "form-control" %>
这将匹配除空格之外的任何内容,将它们放入数组中,然后仅将它们连接到一个空格。工作示例贝娄:
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
//handle PN
}
}
pText1.match(/[^\s]+/g).join(' ');