Javascript 在字符首次出现之前插入字符串

Javascript 在字符首次出现之前插入字符串,javascript,Javascript,我有一个导入图像文件的功能,我想将缩略图与原始文件相关联。我需要在句点第一次出现之前插入\u og。有人能帮我用正则表达式吗,或者知道另一种方法吗 importAll(r) { let imageArray = r.keys(); // This removes the actual original file from the array // so it only includes the thumbnails since they are all in the

我有一个导入图像文件的功能,我想将缩略图与原始文件相关联。我需要在句点第一次出现之前插入
\u og
。有人能帮我用正则表达式吗,或者知道另一种方法吗

importAll(r) {
    let imageArray = r.keys();

    // This removes the actual original file from the array 
    // so it only includes the thumbnails since they are all in the same directory.
    imageArray = imageArray.filter(s => !s.includes('_og'));

    imageArray.forEach(key => (
        this.images.push({
            path: r(key), // thumbnail
            pathOriginal: r(key).replace(/./g , "_og."), // original
        })
    ));
}

是正则表达式中的一个特殊字符,表示匹配任何字符,因此应将其转义:

。替换(/\./g,“\u og.”)

另一方面,我很确定您正在寻找
对象。keys(r)
而不是
r.keys()
,和
r[key]
而不是
r(key)

是正则表达式中的一个特殊字符,意味着匹配任何字符,因此您应该对其进行转义:

。替换(/\./g,“\u og.”)

顺便说一句,我很确定你在寻找
对象。键(r)
而不是
r.keys()
,和
r[key]
而不是
r(key)
,好的,转义是有效的,但是我有多个
字符串,我只想替换第一个出现的,谢谢。@Josh,只需删除
g
键即可替换第一个匹配项。尽管转义有效,但字符串中有多个
,我只想替换第一个匹配项,谢谢。@Josh,只需删除
g
键即可替换第一个匹配项即可