我有以下字符串:
let str = '/user/:username/'
我要提取username
并用harry
替换掉冒号。
我尝试了以下操作:
const regex = /[^:]+(?=:)/g
str.replace(regex, x => console.log(x))
答案 0 :(得分:2)
尝试:/:\w+/
let str = '/user/:username/'
str.replace(/:\w+/, "harry")
// => "/user/harry/"
答案 1 :(得分:2)
let str = '/user/:username/';
let html = str.replace(":username", "harry");
console.log(html);
答案 2 :(得分:1)
var str = '/user/:username/';
var newstr = str.replace(/:username/i, "harry");
print(newstr);
嗨,朋友,这是您要找的东西吗?我在https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/replace
找到了它答案 3 :(得分:1)
您可以这样使用:
function convertSeconds(secT){
var seconds = secT % 60;
var minutes = ((secT - seconds)/60) % 60;
var hours = (secT - seconds - (minutes * 60)) / 3600 % 3600;
//EDIT
var print = "";
If(hours!=0){
print = print + hours + " hours ";
}
if(minutes!=0){
print = print + minutes + " minutes ";
}
if(seconds!=0){
print = print + seconds + " seconds ";
}
alert(print);
}
答案 4 :(得分:1)
在您的正则表达式[^:]+(?=:)
中,您匹配了1次以上而不是冒号,并断言最后应该有一个冒号,导致匹配/user/
如果要使用否定的字符类,可以匹配一个冒号而不是一个正斜杠:
:[^\/]+
const str = `/user/:username/`;
const result = str.replace(/:[^\/]+/, "harry");
console.log(result);