- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图将此字符串转换为正确的大小写,但它不会返回正确的大小写。知道出了什么问题吗? (我没有收到任何错误)。
var convert = "this is the end";
String.prototype.toTitleCase = function () {
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function (match, index, title) {
if (index > 0 && index + match.length !== title.length &&
match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
(title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
title.charAt(index - 1).search(/[^\s-]/) < 0) {
return match.toLowerCase();
}
if (match.substr(1).search(/[A-Z]|\../) > -1) {
return match;
}
return match.charAt(0).toUpperCase() + match.substr(1);
});
};
convert.toTitleCase();
alert(convert);
最佳答案
这一行convert.toTitleCase();
正在丢弃结果。该方法返回正确的结果,但您没有对其执行任何操作。
var original = "this is the end";
String.prototype.toTitleCase = function () {
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function (match, index, title) {
if (index > 0 && index + match.length !== title.length &&
match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
(title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
title.charAt(index - 1).search(/[^\s-]/) < 0) {
return match.toLowerCase();
}
if (match.substr(1).search(/[A-Z]|\../) > -1) {
return match;
}
return match.charAt(0).toUpperCase() + match.substr(1);
});
};
var titleCased = original.toTitleCase();
alert(titleCased);
关于javascript - toTitleCase 函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23228366/
Public Function TitleCase(ByVal strIn As String) Dim result As String = "" Dim culture A
我试图将此字符串转换为正确的大小写,但它不会返回正确的大小写。知道出了什么问题吗? (我没有收到任何错误)。 var convert = "this is the end"; String.proto
我有这个 Java 函数,它应该将字符串 s 转换为首字母大写。它应该向调用者返回 s 的副本,保留 s。 目前,rv 以空字符串结束。谁能告诉我为什么? private static String
我有一个需要转换的字符串,以便将第一个字符转换为大写。使用 ToTitleCase 方法,它工作正常,但有特殊字符的情况除外。 下面是代码和预期结果 String textToConvert= "TE
我想知道是否有人可以提供帮助,我正在尝试使用大写首字母将某些内容从大写更改为小写,我知道并且我可以使用 ToTitleCase - 但我正在努力实现这一目标; 我知道我需要在 ToTitleCas
我有一个字符串“THURSDAY 26th JANUARY 2011”。 当我使用 CultureInfo.ToTitleCase() 对其进行格式化时: var dateString = "THUR
我试图使用 TextInfo.ToTitleCase 将一些名称转换为正确的大小写。它适用于小写和混合大小写的字符串,但对于所有字符均为大写的字符串,它按原样返回输入字符串。 MSDN 文档中没有提及
你能看看我的 sample 吗? 此结果来自以下示例: var str = @"VIENNA IS A VERY BEAUTIFUL CAPITAL CITY."; var title = new C
我正在尝试找出一种使用 toTitleCase 来忽略序数的方法。它按照我希望的方式适用于除序数之外的所有字符串(例如,1st、2nd、3rd 变为 1St、2Nd、3Rd)。 如有任何帮助,我们将不
出于某种原因,R 的 toTitleCase()函数不适用于“all”这个词。任何想法为什么? library(tools) toTitleCase("all") # gives "all" toT
我从他们的 GitHub page 下载了 Humanizer 库的代码当我在查看 TextInfo 类的某些方法时注意到 Intellisense 窗口中出现黄色的“状态警告”图标时,正在测试代码中
我正在重构我的一些旧代码,然后我发现我在某个时候使用了 Character.toTitleCase() 方法并且不禁想知道 Character. toUpperCase() 会更好。 我阅读了他们的描
System.Globalization.TextInfo.ToTitleCase 的 WinRT/WP8 等价物是什么? ? 最佳答案 我不知道为什么不可以,但它在 WinRT 中不可用。关于 MS
我正在使用 solr 的分面,但遇到了一个问题,我希望我可以绕过使用过滤器。 基本上有时一个城镇名称会出现在 SOLR 中 “剑桥” 有时它会通过 “剑桥” 我想在 Solr 中使用过滤器来阻止城镇名
我是一名优秀的程序员,十分优秀!