繁体   English   中英

正则表达式用下划线替换两个指定字符串之间的空格

[英]regex to replace spaces with underscores between two specified strings

我需要一个正则表达式来用一些特定的 json 键值中的下划线替换空格。

我正在使用 gulp-replace 来做替换位。 这是我的纸巾:

const method1 = /(?<=title": ")(\s+)(?=",)/g; // not working 
const method2 = /(?:\G(?!^)|title": ")[^" ]*\K[ ]/g; // works in regex101.com but not when run as part of the gulp replace task below

gulp.task('spaceReplace', function () {
      return gulp
        .src(['src/data/data.json'])
        .pipe(replace(method2, '_'))
       .pipe(gulp.dest('src/data/output/'));
  });

输入(json数据文件):

{
 "stories": {
    "story1_category": "",
    "story1_title": "Blue Monday",
    "story1_link": "",
    "story2_category": "",
    "story2_title": "The Meaning Of Life",
    "story2_link": "",
    "story3_category": "",
    "story3_title": "Blind Mans Bluff",
    "story3_link": "",
    "story4_category": "",
    "story4_title": "The Art Of Fly-Fishing",
    "story4_link": "",
    "story5_category": "",
    "story5_title": "King Of The Hill"
    }
}

期望输出:

{
 "stories": {
    "story1_category": "",
    "story1_title": "Blue_Monday",
    "story1_link": "",
    "story2_category": "",
    "story2_title": "The_Meaning_Of_Life",
    "story2_link": "",
    "story3_category": "",
    "story3_title": "Blind_Mans_Bluff",
    "story3_link": "",
    "story4_category": "",
    "story4_title": "The_Art_Of_Fly-Fishing",
    "story4_link": "",
    "story5_category": "",
    "story5_title": "King_Of_The_Hill"
    }
}

我尝试了一个表达式来匹配每个节点并选择有效的整个值:

(?<=title": ")(.*?)(?=",)

然后我尝试以下更新 - 想法是我正在用一个表达式替换“匹配所有”部分,该表达式只匹配该值中的空格:

(?<=title": ")(\s+)(?=",)

但它不起作用。 为什么上面的不起作用?

我还尝试了基于此的另一种方法

(?:\G(?!^)|title": ")[^" ]*\K[ ]

这在使用https://regex101.com/测试时根据需要匹配,但在https://regexr.com/ 中测试时不匹配,当我尝试将其用作我的method1正则表达式时,它也不起作用任务

为什么上述方法也不起作用? 什么是可以匹配 json 树中每个标题值中的空格的解决方案?

将正则表达式应用于 JavaScript 对象永远不会奏效,将其应用于 JSON(字符串)它可以工作,但当字符串很复杂时会导致不希望的结果。 你想把它分解成小块,只更换需要更换的部件。 所以你想遍历键和值并改变它们:

 const JSON = { "stories": { "story1 category": "", "story1 title": "Blue Monday", "story1 link": "", "story2 category": "", "story2 title": "The Meaning Of Life", "story2 link": "", "story3 category": "", "story3 title": "Blind Mans Bluff", "story3 link": "", "story4 category": "", "story4 title": "The Art Of Fly-Fishing", "story4 link": "", "story5 category": "", "story5 title": "King Of The Hill" } } //use Object.keys to loop over every key Object.keys(JSON.stories).forEach( (item) => { //first update the item's value JSON.stories[item] = JSON.stories[item].replace(/\\s+/g, "_"); //add a new property to stories containing the changed item key const replacedKey = item.replace(/\\s+/g, "_"); //set the item's value to the new key JSON.stories[replacedKey] = JSON.stories[item]; //delete the old key delete JSON.stories[item]; }); console.log(JSON);

将@Mouser 的解决方案应用到我的 gulp 任务中 - 我认为它类似于以下内容,但也失败了。 任何输入都非常感谢:

var storiesData = require('./src/data/stories.json'); 

gulp.task('spaceReplace', function () {
    return Object.keys(storiesData).forEach( (item) => {
        return gulp
            .src(storiesData[item])
            .pipe(replace(/\s+/g, '_'))
            .pipe(gulp.dest('src/data/'));
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM