从文本转换大写中排除某些单词

时间:2018-06-25 07:45:00

标签: html css

我的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大写。 谢谢。

2 个答案:

答案 0 :(得分:1)

一种方法是循环播放所有文本,并检查其默认设置是否较低。对于大文本,此方法很慢

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>

答案 1 :(得分:0)

id更改为class,并用.search类将您要大写的每个单词包装起来

HTML

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

JS:

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

输出

Filter for Customers