[英]How do I make JavaScript's replaceAll() treat strings with both lower case and upper case as if they were lower case?
In my <div contenteditable="true">
, the user can type the word " star " and the word gets immediately replaced by a star symbol.在我的
<div contenteditable="true">
,用户可以输入“ star ”这个词,这个词会立即被一个星号替换。
If the user types "star" in all lower case letters, this will be displayed: ★如果用户输入所有小写字母“star”,将显示:★
If the user types it with all upper case letters, this will be displayed: ☆如果用户输入全部大写字母,则会显示:☆
What I need: If the user types the word "star" with a mixture of lower and upper case letters (examples: staR, Star, STaR...), this should be displayed: ★我需要什么:如果用户输入大小写字母混合的单词“star”(例如:staR、Star、STAR...),则应显示:★
My first thought was using toLowerCase()
, but since the conversion happens onInput
, and it only converts the text once the whole word is typed, using it will make it impossible to type "STAR", all upper case.我的第一个想法是使用
toLowerCase()
,但由于转换发生在onInput
,并且只有在输入整个单词后才会转换文本,因此使用它将无法输入“STAR”,全部为大写。
Any ideas?有任何想法吗?
I am open to jQuery
solutions.我对
jQuery
解决方案持开放jQuery
。
Here is my code:这是我的代码:
// Autofocus the div: document.getElementById("editableDiv").focus(); // Makes the conversion possible: String.prototype.allReplace = function(obj) { var retStr = this; for (var x in obj) { retStr = retStr.replace(new RegExp(x, 'g'), obj[x]); } return retStr; }; // Converts function convert() { var str = document.getElementById("editableDiv").innerHTML; var white = str.allReplace({ 'STAR': '☆', 'DIAMOND': '◇' }); var black = white.allReplace({ 'star': '★', 'diamond': '◆' }); document.getElementById("editableDiv").innerHTML = black; // Move caret to the end: var contentEditableElement = document.getElementById("editableDiv"); var range, selection; if (document.createRange) { range = document.createRange(); range.selectNodeContents(contentEditableElement); range.collapse(false); selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); } else if (document.selection) { range = document.body.createTextRange(); range.moveToElementText(contentEditableElement); range.collapse(false); range.select(); } }
body { padding: 10px; font-family: sans-serif; } div { padding: 5px 20px; width: 200px; border: solid 1px #000; outline: 0; line-height: 2; font-size: 1.5em; }
<div id="editableDiv" contenteditable spellcheck="false" oninput="convert();"></div>
First I would recommand to use the existant replaceAll .首先,我建议使用现有的replaceAll 。
Second you ARE using regexp !其次,您正在使用正则表达式! Don't worry it's not that complicated ( for your purpose ) I suggest you to do so in 2 steps :
别担心,它并没有那么复杂(为了您的目的)我建议您分两步完成:
1 - Replace all 'STAR' with ☆ 1 - 用 ☆ 替换所有 'STAR'
2 - Then replace " without taking care of the case " 'star' with ★ 2 - 然后用 ★ 替换“不关心案件”“星”
And " without taking care of the case " means just to use the flag i
in the regexp ;)并且“不处理这种情况”意味着只是在正则表达式中使用标志
i
;)
function replaceMyStuff(str) { var r = str.replaceAll('STAR', '☆') .replaceAll(/star/gi, '★') .replaceAll('DIAMOND', '<>') .replaceAll('stuff', 'replacedStuff'); return r; } console.log(replaceMyStuff('star STAR StAr STAR STAr stuff'));
If your are more confortable with it you can write :如果你对它更舒服,你可以写:
new RegExp('star', 'gi')
It's basically the same as /star/gi
基本和
/star/gi
Then you can use it to replace all that you want :然后你可以用它来替换你想要的所有内容:
var str = document.getElementById("editableDiv").innerHTML;
var replacedString = replaceMyStuff(str);
You can run checks against a target string using the strict equality comparison ( ===
) operator, like this:您可以使用严格相等比较 (
===
) 运算符对目标字符串运行检查,如下所示:
if (string.toLowerCase() === 'star') {
if (string === 'STAR') {
output = '☆';
}
else {
output = '★';
}
}
Working Example:工作示例:
const paragraphs = [...document.getElementsByTagName('p')]; const convertParagraph = (paragraph) => { let paragraphArray = paragraph.textContent.split(' '); for (let i = 0; i < paragraphArray.length; i++) { if (paragraphArray[i].toLowerCase() === 'star') { if (paragraphArray[i] === 'STAR') { paragraphArray[i] = '☆'; } else { paragraphArray[i] = '★'; } } } paragraph.textContent = paragraphArray.join(' '); } paragraphs.forEach(paragraph => convertParagraph(paragraph));
<div> <p>STAR This is a test.</p> <p>star This is another test.</p> <p>sTAr This is a third test.</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.