简体   繁体   English

用jQuery使大写的特定单词(字符串)

[英]make particular word (string) in caps with jquery

I have a word "Real" in the website I don't want to search it page by page and make it caps. 我在网站上有一个词“ Real”,我不想一页一页地搜索它并使其大写。

I am trying to make "Real" word in caps through jquery and I am using the below code: 我正在尝试通过jquery在大写中使用“ Real”一词,并且我使用以下代码:

$('jqueryselector').val($(this).val().toUpperCase());
<html> code Real Change Fellowship

The above code is making whole text in caps and i just want to make "Real" word in caps. 上面的代码使整个文本大写,而我只想使大写为“ Real”。

Please help. 请帮忙。

In case your code above is working ( .val() , really?), in order to uppercase just "real" and not the whole string, you'd need to do a string replace: 如果上面的代码可以正常工作( .val() ,真的吗?),为了将“ real”大写而不是整个字符串大写,您需要执行字符串替换:

$(this).val().replace(/real/gi, 'REAL');

As pointed out by Jaromanda in comments, the actual expression could be improved upon, pending your exact requirements, but this would be the general idea. 正如Jaromanda在评论中指出的那样,可以根据您的确切要求来改进实际的表达方式,但这只是一般性的想法。

There are conceptual issues with this, however, if you're doing this because "Real" is an abbreviation. 但是,如果这样做是有概念上的问题,因为“ Real”是缩写。 You will never be able to definitely distinguish the abbreviation from all uses of the English word "real". 您将永远无法从英文单词“ real”的所有用法中完全区分该缩写。 If at all an option, I would definitely consider getting the source content right rather than trying to patch it up post hoc. 如果有选择的话,我绝对会考虑正确地获取源内容,而不是事后进行修补。 Most IDE's should come with a "search and replace in all files" functionality. 大多数IDE应该带有“搜索并替换所有文件”功能。

以下将把“真实”一词更改为“真实”

 $('jqueryselector').val($(this).val().replace(\bReal\b/gm, 'REAL'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM