Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript只替换管道前的点,而不替换管道后的点_Typescript - Fatal编程技术网

Typescript只替换管道前的点,而不替换管道后的点

Typescript只替换管道前的点,而不替换管道后的点,typescript,Typescript,我有一个包含点的字符串,我想用空格替换它们,例如: i、 爱。狗。因为它很好 不幸的是,它只是在管道之前而不是之后替换点。这是我的密码: let id = blogId.replace(".", " ") 您需要使用设置了g(全局)标志的正则表达式,以第二个参数替换它的所有实例 因此,您需要: const blogId = "i.love.dogs.|.because.its.nice" let id = blogId.replace(/\./g, " ") // now id

我有一个包含点的字符串,我想用空格替换它们,例如:

i、 爱。狗。因为它很好

不幸的是,它只是在管道之前而不是之后替换点。这是我的密码:

      let id = blogId.replace(".", " ")

您需要使用设置了
g
(全局)标志的正则表达式,以第二个参数替换它的所有实例

因此,您需要:

const blogId = "i.love.dogs.|.because.its.nice"
let id = blogId.replace(/\./g, " ")

// now id is "i love dogs | because its nice"
有关详细信息,请参阅文档

replace()方法返回一个新字符串,其中包含替换所替换模式的部分或全部匹配项。模式可以是字符串或RegExp,替换可以是字符串或为每个匹配调用的函数如果模式是字符串,则仅替换第一个匹配项

您可以使用正则表达式模式实现所需的结果:

var result=blogId.replace(/\./g,”)

或者您可以编写一个助手方法:

//replace all dots in string str with a space
stripDots(str: string): string {
    let result = ""
    for (var c of str){
        if (c === ".")
            c = " ";
        result += c;
    }
    return result;
}

:如果模式是字符串,则仅替换第一个匹配项。[…]要执行全局搜索和替换,请在正则表达式中包含g开关。