/hai1world/ 有什么线索吗?-6ren">
gpt4 book ai didi

javascript - 字符串操作: How to add variable in//pattern?

转载 作者:行者123 更新时间:2023-11-28 13:45:54 24 4
gpt4 key购买 nike

var num = 1
var pattern = /hai/+num+/world/

它打印:

> "/hai/1/world/"

虽然我希望它打印这个:

> /hai1world/

有什么线索吗?

最佳答案

var num = 1;
var pattern = new RegExp('hai' + num + 'world');

您不能将 /.../ 与变量一起使用。

关于javascript - 字符串操作: How to add variable in//pattern?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430218/

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