簡體   English   中英

用於在不帶引號的值周圍添加引號的正則表達式(數字和布爾值)

[英]Regex for add quotes around values without quotes (number and boolean)

I receive a valid JSON file, but I would like to add " around the number values and the boolean values to send this JSON in a wix repeater which asks for " doubles around the number and boolean values

如果有人可以幫助我請

示例 JSON 收到:

[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1}]

需要示例 JSON:

[{"id":"1238890","category_id":"1","season_id":"14866","venue_id":"null","referee_id":"null","slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":"0","max":"2700","timestamp":"1660296540","extra":"540"}, "_id":"1"}]

我嘗試了下面的代碼來轉換 json,但沒有得到想要的結果

J'ai essayé ce code là en transformant mon json en chaine string mais sans le résultat escompté..

const regex = /[^"\d,]?(\d+)/g;
const str = [{"id":"1238890","category_id":"1","season_id":"14866","venue_id":"null","referee_id":"null","slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":"0","max":"2700","timestamp":"1660296540","extra":"540"}, "_id":"1"}]

const subst = `:` + `"$1"`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

親切地

解析 json,使用replacer器 function 重新字符串化(參見MDN )。 下面的代碼段還引用了null

 const json = JSON.parse(`[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1, "someBoolean":false}]`); console.log(JSON.stringify(json, (key, value) => /number|boolean/.test(typeof value) || value === null? `${value}`: value, 2));
 .as-console-wrapper { max-height: 100%;important; }

我寧願只轉換 object 而不是使用正則表達式

 const json = `[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1}]` const data = JSON.parse(json) const converted = convertToStrings(data) console.log(converted) function convertToStrings(obj) { if (['boolean', 'number'].includes(typeof obj)) { return obj.toString() } if (obj === null) { return 'null' } if (typeof obj === 'string') { return obj } if (Array.isArray(obj)) { const newArray = [] for (const item of obj) { newArray.push(convertToStrings(item)) } return newArray } if (typeof obj === 'object') { const newObj = {} for (const key in obj) { newObj[key] = convertToStrings(obj[key]) } return newObj } throw new Error('Unsupported property type ' + typeof obj) }

正如其他人指出的那樣,將 JSON 字符串轉換為 object,操作 object,然后轉換回 Z0ECD1814C1D7A3BB7A22 字符串是很好的方法。

這是一個簡短的正則表達式,用於直接更改 JSON 字符串:

 const jsonStr = '[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1, "someBoolean":false}]'; const re = /(":\s*)(\d+|true|false|null)(,\s*"|\s*[\}\]])/g; console.log(jsonStr.replace(re, '$1"$2"$3'));

正則表達式的解釋:

  • (":\s*) -- 捕獲組 1:轉義值之前的預期模式
  • (\d+|true|false|null) -- 捕獲組 1:要轉義的值
  • (,\s*"|\s*[\}\]]) -- 捕獲組 1:轉義值后的預期模式
  • /g -- 多次替換

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM