- substr() :
if("hello world".substr(0, 5) == "hello"){
console.log(true);
}
- substring() :
if("hello world".substring(0, 5) == "hello"){
console.log(true);
}
- slice():
if("hello world".slice(0, 5) == "hello"){
console.log(true);
}
- indexOf():
if("hello world".indexOf("hello") == 0) {
console.log(true);
}
- search()
if("hello world".search("hello") == 0) {
console.log(true);
}
- test()
if(new RegExp("^abc.*$").test("abcdefg")) {
console.log(true);
}
- match()
if("abcdefg".match(new RegExp("^ab.*$"))) {
console.log(true);
}
返回数组最大值和最小值
let array=[1,2,3,4,5,6,7,8,9,0]
//查询最大值
var maxValue = Math.max.apply(Math, array);
//查询最小值
var minValue = Math.min.apply(Math, array);
最新小程序案例

这篇博客探讨了JavaScript中常见的字符串操作方法,如substr(), substring(), slice()以及indexOf()和search()的使用,并通过实例展示了它们在判断字符串前缀时的等效性。同时,文章还介绍了如何利用Math.max()和Math.min()找到数组中的最大值和最小值。这些基础知识对于前端开发者和数据处理至关重要。
1063

被折叠的 条评论
为什么被折叠?



