简体   繁体   中英

regex to replace spaces with underscores between two specified strings

I need a regex to replace spaces with underscores in some specific json key values.

I am using gulp-replace to do the replace bit. This is my wip:

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/'));
  });

Input (json data file):

{
 "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"
    }
}

Desired Output:

{
 "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"
    }
}

I tried an expression to match each node and select the whole value which works:

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

I then try the following update - the idea being I'm replacing the part to 'match all' with an expression which is looking to match only spaces within that value:

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

But it doesn't work. Why does the above not work?

I also tried another method based on this

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

This matches as desired when tested with https://regex101.com/ but does not match when tested in https://regexr.com/ and it also doesn't work when I try to use it as my method1 regex in my gulp task

Why does the above not work either? And what is a solution that will work to match on the spaces in each title value in the json tree?

Applying a regex to a JavaScript Object will never work, and applying it to the JSON (string) it could work, but lead to undesired result when the string is complex. You want to break it down into small pieces and replace only the parts that need to be replaced. So you want to iterate over the keys and the values and change them:

 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);

Applying @Mouser's solution into my gulp task - I figure it's something like the following but failing on this one too. Any input much appreciated:

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/'));
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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