我如何String.StartsWith在JavaScript中编写等效的C#?
var haystack = 'hello world';
var needle = 'he';
haystack.startsWith(needle) == true
Run Code Online (Sandbox Code Playgroud)
注意:这是一个老问题,正如评论中指出的ECMAScript 2015(ES6)引入了该.startsWith方法.但是,在撰写此更新时(2015年),浏览器支持还远未完成.
我有以下内容:
if (referrer.indexOf("Ral") == -1) { ... }
Run Code Online (Sandbox Code Playgroud)
我喜欢做的事情就是让Ral不区分大小写,因此,它可以是RAl,rAl等,仍然匹配.
有没有办法说Ral必须不区分大小写?
我需要从URL创建一个Javascript数组,例如:
转过来:
http://maps.google.com/maps/api/staticmap?center=Baker Street 221b, London&size=450x450&markers=Baker Street 221b, London&sensor=false
Run Code Online (Sandbox Code Playgroud)
变成这样的东西:
array['center'] = Baker Street 221b, London
array['size'] = 450x450
// and so on...
Run Code Online (Sandbox Code Playgroud)
我需要使这个序列化/反序列化双向工作(url到数组和数组到url的部分).是否有一些内置函数可以做到这一点?
提前致谢!
这是现有问题的延续. Javascript - 根据下拉选项转到URL(续!)
我使用下拉选项允许我的用户构建一个URL,然后点击"Go"转到它.
是否有任何方法可以添加一个额外的功能来检查URL之前的URL?
我的URL有时包含"+"字符,如果它是URL中的最后一个字符,我需要删除它.所以它基本上需要"如果最后一个字符是+,删除它"
这是我的代码:
$(window).load(function(){
$('form').submit(function(e){
window.location.href =
$('#dd0').val() +
$('#dd1').val()+
$('#dd2').val()+
$('#dd3').val();
e.preventDefault();
});
});
Run Code Online (Sandbox Code Playgroud) 我需要根据URL末尾的内容向下滑动叠加层.
如果(URL末尾有'faq'){overlay down down}
你怎么能在jQuery/JavaScript中做到这一点?
我有一个字符串
var s1 = "a,$,b,c";
Run Code Online (Sandbox Code Playgroud)
我想检查另一个字符串是否以 s1
所以,如果我发送这些字符串,它必须返回 true
w,w,a,$,b,c
^,^,^,$,@,#,%,$,$,a,$,b,c
a,w,e,q,r,f,z,x,c,v,z,$,W,a,$,b,c
Run Code Online (Sandbox Code Playgroud)
对于这些 false
a,$,b,c,F,W
a,$,b,c,W
a,$,b,c,$,^,\,/
Run Code Online (Sandbox Code Playgroud)
我怎么检查呢?
我有一个Web应用程序。在其中一个页面中,我遍历所有HTML元素ID,无论其中一个是否以指定的字符串结尾。每个JS函数都可以在页面上运行,但是“ endsWith”函数不起作用。我真的不明白这件事。有人可以帮忙吗?
var str = "To be, or not to be, that is the question.";
alert(str.endsWith("question."));
Run Code Online (Sandbox Code Playgroud)
上面的简单JS代码根本行不通吗?
此任务要求您编写一个带有两个参数的函数.第一个参数是一个被调用str的字符串,第二个参数是一个我们的目标结尾命名的字符串target.任务是验证结尾str与目标结尾相同.说明书指示使用该.substr()方法将结束与目标进行比较.我遇到的问题是该方法将有多个起始点和长度参数,.substr因为目标结尾可以是可变长度.看看我尝试解决这个问题,请指导我走正确的道路.
function end(str, target) {
var start = str.length - (target.length - 1);
if(str.substr(start, str.length) == target){
return true;
} else {
return false;
}
}
end('Bastian', 'n');
Run Code Online (Sandbox Code Playgroud) 我在正则表达式方面不擅长,我只是想问一下如果它结束了"\ r \n"我怎么能检查字符串?
先感谢您.
var file = "{employee}";
var imgFile = "cancel.json";
if(file starts with '{' and file ends with '}' ){
alert("invalid");
}
if(imgFile ends with '.json'){
alert("invalid");
}
Run Code Online (Sandbox Code Playgroud)
我在自定义的grunt任务中遇到了错误.下面我发布了一个与问题相关的简单测试用例:
Gruntfile.js
module.exports = function( grunt ){
grunt.task.registerTask( 'endsWith', 'Test of string.prototype.endsWith', function(){
var value = grunt.option('value');
grunt.log.writeln( typeof value );
grunt.log.writeln( value.endsWith( 'bar' ) );
})
};
Run Code Online (Sandbox Code Playgroud)
测试
> grunt endsWith --value=foobar
Running "endsWith" task
string
Warning: Object foobar has no method 'endsWith' Use --force to continue.
Aborted due to warnings.
Execution Time (2016-02-12 16:15:19 UTC)
Total 12ms
Run Code Online (Sandbox Code Playgroud)
就像grunt无法识别String.proptotype.endsWith函数一样.这是正常的吗?
编辑:我正在使用节点v0.10.4
我想检查一个字符串是否以
- v{number}
Run Code Online (Sandbox Code Playgroud)
所以例如
hello world false
hello world - v2 true
hello world - v false
hello world - v88 true
Run Code Online (Sandbox Code Playgroud)
不完全确定如何去做这个 RegEx。
var str = 'hello world - v1';
var patt = new RegExp("/^ - v\d*$/");
var res = patt.test(str);
console.log(res);
Run Code Online (Sandbox Code Playgroud)
我怎么能修复上面的正则表达式?
javascript ×13
string ×6
jquery ×2
url ×2
gruntjs ×1
regex ×1
startswith ×1
substring ×1
validation ×1