gpt4 book ai didi

Javascript/JQuery 条件替换

转载 作者:行者123 更新时间:2023-11-28 10:54:09 32 4
gpt4 key购买 nike

我在 javascript 中有一个变量:

var myvar = "0711111111";

我基本上需要做一个条件:

if (myvar's first character = '0') or (myvar's first character = '+') {

then ... replace the first character with 'somethingelse';

}

最佳答案

请尝试这个!

var myvar =     "0711111111" , repWithChar  =   "x";
var firstChar = myvar.charAt(0);
if ((firstChar === "0") || (firstChar === "+")) {
console.log( myvar.replace(myvar.charAt(0), repWithChar) );
}

输出:x711111111

关于Javascript/JQuery 条件替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226888/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com