我在段落中用连字符分隔了我想要提取的单词.
我试过跟随正则表达式\w+-\w,但是这没有按预期工作.
这是用JavaScript编写的完整代码.
var string = "time to eval-u-ate";
var result = string.match(/\w+-\w/g); // ["eval-u"]
Run Code Online (Sandbox Code Playgroud)
这将返回字符串eval-u.我想要结果eval-u-ate.如何修改正则表达式以匹配完整的带连字符的单词.
我希望这些颜色按照给定的颜色顺序循环.但它给了我所有div上只有绿色.
var colors = ["red", "blue", "green"];
for (var i = 0; i < colors.length; i++) {
$("div").each(function() {
$("div").css({
'background-color': colors[i]
});
});
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div>A</div>
<div>B</div>
<div>C</div>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在数组中找到单个出现的元素。但它只显示 1 个元素。逻辑错在哪里?
function findSingle(array){
var arrayCopy = array.slice(0);
var x;
var y = [];
for (var i = 0; i < array.length; i++) {
x = arrayCopy.splice(i, 1)
if(arrayCopy.includes(array[i]) === false){
console.log(array[i] + " is single")
}
arrayCopy = arrayCopy.concat(x)
}
}
findSingle([1, 3, 3, 6])
Run Code Online (Sandbox Code Playgroud) 这甚至可能吗?我想以不同的间隔使用相同的功能.如何使函数在5000运行并执行函数中的某些代码部分.
这是一个给出一个想法的例子:
var moto = function(){
// at 5000 run this part of code
console.log("did stuff when called at 5000 time interval")
// at 6000 run this part of code
console.log("did stuff when called at 6000 time interval")
}
window.setInterval(moto, 5000)
window.setInterval(moto, 6000)
Run Code Online (Sandbox Code Playgroud) 我需要的只是apple数组中的from 对象。
const fruits = [{name: "apple"}, {name: "banana"}]
fruits.map((f)=>{
console.log(f.name[0])
})
Run Code Online (Sandbox Code Playgroud)