Jquery 使用不区分大小写的 indexOf 在数组中搜索
Jquery make searching in array with indexOf case insensitive
我有这个 function
,它会在输入时检查文本区域是否包含字符串 :tag:
。如果是,则将其替换为 [tag] [/tag]
。例如,如果我们键入 :b:
,它将插入 [b] [/b]
.
这一切都很好,但即使我们使用大写字母,我也想让它工作。就像输入 :B:
也会插入 [b] [/b]
(或 [B] [/B]
,这并不重要),但无论我如何尝试,它都不起作用。
当然,它需要与每个标签一起工作。
$("textarea").on("input",function(e){
let textarea=$(this);
let text = textarea.val();
let target=e.target;
let position=target.selectionStart;
let tags=["B","I","U","S","Q",
"H1","H2","H3","H4","H5","H6",
"LEFT","CENTER","RIGHT","SUB","SUP",
"H","HR","CODE","SPOILER","UL","OL","LI"];
$.each(tags, function(index, tag) {
tag=tag.toLowerCase();
let fullTag=":"+tag+":";
if(textarea.val().toLowerCase().indexOf(fullTag) > -1 ){
textarea.val(text.replace(fullTag, "["+tag+"] [/"+tag+"]"));
target.selectionEnd=position+2;
}
});
});
textarea{
width:200px;
height:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>
我觉得答案其实很简单,我搜索了很多,尝试了很多变体,但我还是想不通。如果你能帮我解决这个问题,我会很感激。
从 tags
数组中,我构建了一个不区分大小写的正则表达式,并在 replacer 函数中,检查整个字符串(最后一个参数)中匹配项的索引以确定位置设置新的 selectionEnd
:
const tags = ["B", "I", "U", "S", "Q",
"H1", "H2", "H3", "H4", "H5", "H6",
"LEFT", "CENTER", "RIGHT", "SUB", "SUP",
"H", "HR", "CODE", "SPOILER", "UL", "OL", "LI"
];
const pattern = new RegExp(tags.map(tag => `:${tag}:`).join('|'), 'i');
$("textarea").on("input", function(e) {
let usedIndex = null;
this.value = this.value.replace(
pattern,
(match, index) => {
const tag = match.match(/\w+/)[0];
usedIndex = index + tag.length + 4;
return `[${tag}] [${tag}]`
}
);
if (usedIndex) {
this.selectionEnd = usedIndex;
}
});
textarea {
width: 200px;
height: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>
我有这个 function
,它会在输入时检查文本区域是否包含字符串 :tag:
。如果是,则将其替换为 [tag] [/tag]
。例如,如果我们键入 :b:
,它将插入 [b] [/b]
.
这一切都很好,但即使我们使用大写字母,我也想让它工作。就像输入 :B:
也会插入 [b] [/b]
(或 [B] [/B]
,这并不重要),但无论我如何尝试,它都不起作用。
当然,它需要与每个标签一起工作。
$("textarea").on("input",function(e){
let textarea=$(this);
let text = textarea.val();
let target=e.target;
let position=target.selectionStart;
let tags=["B","I","U","S","Q",
"H1","H2","H3","H4","H5","H6",
"LEFT","CENTER","RIGHT","SUB","SUP",
"H","HR","CODE","SPOILER","UL","OL","LI"];
$.each(tags, function(index, tag) {
tag=tag.toLowerCase();
let fullTag=":"+tag+":";
if(textarea.val().toLowerCase().indexOf(fullTag) > -1 ){
textarea.val(text.replace(fullTag, "["+tag+"] [/"+tag+"]"));
target.selectionEnd=position+2;
}
});
});
textarea{
width:200px;
height:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>
我觉得答案其实很简单,我搜索了很多,尝试了很多变体,但我还是想不通。如果你能帮我解决这个问题,我会很感激。
从 tags
数组中,我构建了一个不区分大小写的正则表达式,并在 replacer 函数中,检查整个字符串(最后一个参数)中匹配项的索引以确定位置设置新的 selectionEnd
:
const tags = ["B", "I", "U", "S", "Q",
"H1", "H2", "H3", "H4", "H5", "H6",
"LEFT", "CENTER", "RIGHT", "SUB", "SUP",
"H", "HR", "CODE", "SPOILER", "UL", "OL", "LI"
];
const pattern = new RegExp(tags.map(tag => `:${tag}:`).join('|'), 'i');
$("textarea").on("input", function(e) {
let usedIndex = null;
this.value = this.value.replace(
pattern,
(match, index) => {
const tag = match.match(/\w+/)[0];
usedIndex = index + tag.length + 4;
return `[${tag}] [${tag}]`
}
);
if (usedIndex) {
this.selectionEnd = usedIndex;
}
});
textarea {
width: 200px;
height: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>