gpt4 book ai didi

html - 从 text-transform capitalize 中排除某些单词

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:01 24 4
gpt4 key购买 nike

我的 HTML

<span id="search">filter for customers</span>

SCRIPT

$('#search').css('text-transform','capitalize');

这给了我结果

<span id="search">Filter For Customers</span>

我的问题是如何排除“for”一词的大写。这样的结果一定是

<span id="search">Filter for Customers</span>

请注意,这只是一个例子,我有一个不能大写的单词列表。我正在寻找一些动态解决方案,无论何时发现这些单词都不应该被 text-transform 大写。谢谢。

最佳答案

一种方法是循环所有文本并检查它是否是默认值。这种方法对于大文本很慢

String.prototype.ucFirst = function()
{
return this.charAt(0).toUpperCase() + this.substr(1);
};

$(document).ready(function () {
var lowerWords = [
'for', 'to'
];

$('.uc-first').each(function () {
var words = $(this).text().split(' ');
pattern = /([^a-z0-9]*)(\w+)([^\w]*)/i

$.each(words, function (i, value) {
mathches = pattern.exec(value);
if (lowerWords.indexOf(mathches[2]) == -1) {
words[i] = mathches[1]+mathches[2].ucFirst()+mathches[3];
}
});

$(this).text(words.join(' '));
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="uc-first">some text for SO Community to investigate</span><br/>
<span class="uc-first">test ;for. additional symbols;</span><br/>
<span class="uc-first">filter for customers</span>

关于html - 从 text-transform capitalize 中排除某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51018464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com