我只想用任何可能的字符串创建一个正则表达式.
var usersString = "Hello?!*`~World()[]";
var expression = new RegExp(RegExp.escape(usersString))
var matches = "Hello".match(expression);
Run Code Online (Sandbox Code Playgroud)
有内置的方法吗?如果没有,人们会用什么?Ruby有RegExp.escape.我觉得我不需要自己编写,那里必须有标准的东西.谢谢!
我有一个我希望根据attr字符串类型字段排序的对象列表.我试过用-
list.sort(function (a, b) {
return a.attr - b.attr
})
Run Code Online (Sandbox Code Playgroud)
但发现-在JavaScript 中似乎不适用于字符串.如何根据类型字符串的属性对对象列表进行排序?
我正在尝试使用JavaScript中的两个字符串进行不区分大小写的搜索.
通常它会是这样的:
var string="Stackoverflow is the BEST";
var result= string.search(/best/i);
alert(result);
Run Code Online (Sandbox Code Playgroud)
该/i标志将不区分大小写.
但我需要搜索第二个字符串; 没有旗帜它完美无缺:
var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(searchstring);
alert(result);
Run Code Online (Sandbox Code Playgroud)
如果我将/i标志添加到上面的示例中,它将搜索searchstring而不是变量"searchstring"中的内容(下一个示例不起作用):
var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(/searchstring/i);
alert(result);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有jQuery,但我不确定它是否有任何内置的排序助手.我可以让每个项目的一个二维数组text,value和selected属性,但我不认为JavaScript的内置Array.sort()将正常工作.
我必须使用JavaScript检查一些字符串,但区分大小写会导致问题.例如
if('abc'=='ABC')
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
尽管单词的含义相同,但它不会进入if循环.我也不能使用tolower条款,因为我不知道数据如何来它对于ex意味着:
if('aBc'=='abC')
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果可以通过jquery完成,如何为此编写JS函数.
我有一个input保存URL 的字段,我希望这个保存的输入能够识别变量开头没有"Http //"但不知道从哪里开始...是否可以只检查一部分一串? - 然后有必要附加的功能?
在JavaScript中,我正在尝试使用用户的输入来搜索我的数据库.例如,用户输入是"怪物",我的数据库的数据是"怪物".无论它的外壳如何,我怎么能匹配它?
我的网站上有一个文本框,用户可以输入命令.但问题是命令区分大小写.如何使命令不区分大小写?这是我的代码:
JavaScript的:
function showAlert() {
var txtCtrl = document.getElementById("textbox1");
var txtVal = txtCtrl.value;
if (txtVal == '') {
alert('Please fill in the text box. For a list of commands type "Help" into the text box.');
}else if (txtVal == 'Start' || txtVal == 'start') {
alert('Hello. What would you like me to do?');
}else if (txtVal === 'Weather' || txtVal === 'weather') {
window.location = "https://www.google.com/#q=weather";
}else if (txtVal === 'Time' || txtVal === 'time') {
alert('The current time according …Run Code Online (Sandbox Code Playgroud) 我正在检查字符串输入是否包含任何字符串数组。它通过了大部分测试,但没有通过以下测试。
谁能分解我的代码,为什么它不能正常工作?
function checkInput(input, words) {
var arr = input.toLowerCase().split(" ");
var i, j;
var matches = 0;
for(i = 0; i < arr.length; i++) {
for(j = 0; j < words.length; j++) {
if(arr[i] == words[j]) {
matches++;
}
}
}
if(matches > 0) {
return true;
} else {
return false;
}
};
checkInput("Visiting new places is fun.", ["aces"]); // returns false // code is passing from this test
checkInput('"Definitely," he said in a matter-of-fact tone.',
["matter", "definitely"])); …Run Code Online (Sandbox Code Playgroud) 我正在比较Angular 2模板中的多个字段,它适用于same case属性,但false在不同情况下会返回相同的字符串。有没有办法case insensitive通过简单的方法做到这一点pipe?
<div *ngIf="query?.firstName == address.foreName"></div>
Run Code Online (Sandbox Code Playgroud)