英文:
how to remove first two character from string and space
问题
如果有人帮我。
我尝试用以下语法将第一个"aa"和"ab"替换为空格:
replace(/^(.){2}/,'').trim(),但不起作用。
就像我尝试删除第一个"aa"和"ab",只打印"hello world",但使用这个语法不起作用:
let check = 'aa hello world'
let check = 'ab hello world'
英文:
If any body help me out.
i tried two replace first aa and ab with space with this syntax
replace(/^(.){2}/,'').trim() but not work.
like i have tried to remove first aa and ab only print hello word but not work with this syntax
let check = 'aa hello world'
let check = 'ab hello world'
答案1
得分: 3
以下是一个例子,只需添加一个IF语句来检查字符串是否以aa或ab开头:
var check = 'aa 你好 世界';
if (check.startsWith('aa') || check.startsWith('ab')) {
check = check.substring(3);
}
你甚至可以保留你的代码:
var check = 'aa 你好 世界';
if (check.startsWith('aa') || check.startsWith('ab')) {
check = check.replace(/^(.){2}/,'').trim()
}
console.log(check)
答案:
"你好 世界"
英文:
Here is an example just adding an IF statement to see if the string starts with aa or ab:
var check = 'aa hello world';
if (check.startsWith('aa') || check.startsWith('ab')) {
check = check.substring(3);
}
You can even keep your code:
var check = 'aa hello world';
if (check.startsWith('aa') || check.startsWith('ab')) {
check = check.replace(/^(.){2}/,'').trim()
}
console.log(check)
Answer:
"hello world"
答案2
得分: 0
你可以尝试使用[slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice)
let text = "aa 你好,世界!";
if (text.startsWith('aa') || text.startsWith('ab')) {
text = text.slice(3);
}
结果:
你好,世界!
英文:
Can you try using slice
let text = "aa hello world!";
if (text.startsWith('aa') || text.startsWith('ab')) {
text = text.slice(3);
}
Result:
hello world
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论