簡體   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