繁体   English   中英

如果在 javascript 中的大括号内有任何带方括号的占位符,如何将字符串更改为新字符串?

[英]How can I change a string into new string if it has any placeholders with square brackets inside curly brackets in javascript?

我从数据库中获取 parameterisedString 的值,并且 parameterisedString 在同一字符串中包含一些在大括号内没有方括号的占位符,例如 {Group} 和一些在大括号内带方括号的占位符,例如 {termMonth [month]}。

示例: parameterisedString='{Group} {Desc} - {termMonth [month]} - {termOdometer [kms]} {[$] DeductibleAmount [version]} {DeductibleType}';

仅带大括号的占位符(例如 {Group})不需要替换或更改,但每个在大括号内带有方括号的占位符(例如 {termMonth [month]})应转换为 {termMonth} 占位符和里面的文本方括号应传递给 getTranslation function 以获取该单词的翻译,不应进行插值。 例如 {termMonth [month]}:{termMonth} 应替换或更改为 {termMonth},并且“month”应替换为该单词的 Translation。

现在需要的是:
parameterisedString = '{Group} {Desc} - {termMonth} mois - {termOdometer} kms USD$ {DeductibleAmount} couverture {DeductibleType}';

 buildTitle(dealProduct: DealProductModel, tittleSuffix: boolean = false) { //Example: let parameterisedString='{Group} {Desc} - {termMonth [month]} - {termOdometer [kms]} {[$] DeductibleAmount [version]} {DeductibleType}'; const pairs = [ ['{Group}', `${dealProduct.name}`], ['{Desc}', `${dealProduct.coverageName}`], ['{termMonth}', `${dealProduct.termMonths}`], ['{termOdometer}', `${dealProduct.termOdometer.toLocaleString()}`], ['{DeductibleAmount}', `${dealProduct.deductibleAmount} Deductible`], ['{DeductibleType}', `${dealProduct.deductibleType}`] ]; //HERE I NEED the changed parameterisedString and it should have all the words translated in square brackets before I interpolate. The desired should be now: parameterisedString = '{Group} {Desc} - {termMonth} mois - {termOdometer} kms USD$ {DeductibleAmount} couverture {DeductibleType}'; // interpolate the string let key, val; for (let i = 0; i < pairs.length; i++) { [key, val] = pairs[i]; parameterisedString = parameterisedString.replace(`${key}`, `${val}`); } return parameterisedString; } getTranslation(word: string) { ----- ----- return TranslatedWord; }

现在应该是:
parameterisedString = '{Group} {Desc} - {termMonth} mois - {termOdometer} kms USD$ {DeductibleAmount} couverture {DeductibleType}';

您可以使用正则表达式查找所有出现的 {TERM},然后将其拆分,如果单词以 '[' 开头,则将其替换为翻译,如果不是在 {} 之间返回它

 function changeTemplate(template) { return template.replace(/{(.*?)}/g, (match, template) => { return template.split(/\s+/).map(word => { return word.startsWith('[')? getTranslation(word.slice(1, -1)): `{${word}}` }).join(' '); }) } // mock translation const translations = { month: 'mois', kms: 'kms', $: 'USD$', version: 'couverture' }; function getTranslation(string) { return translations[string] || string; } let parameterisedString='{Group} {Desc} - {termMonth [month]} - {termOdometer [kms]} {[$] DeductibleAmount [version]} {DeductibleType}'; console.log(changeTemplate(parameterisedString));

对于特定的{DeductibleAmount} {[$] DecuctibleType [coverage]} {DeductibleAmount [$]} {DeductibleType [coverage]}字符串的 rest 的格式不同。我有你的答案

 let parameterisedString = '{Group} {Desc} - {termMonth [month]} - {termOdometer [kms]} {DeductibleAmount [$]} {DeductibleType [coverage]}' pm=parameterisedString.split('') var arr=[] let translate=arrOfText=>arrOfText //receives an array of text that would have spacebar then the characters of a term in square bracket.. for example, [" ","m","o","n","t","h"] while(pm.includes('[')&&pm.includes(']')){ var curlyVar=pm.splice(0,pm.indexOf('[')-1) curlyVar.push('}') var bracketVar=pm.splice(pm.indexOf('[')-1,pm.indexOf(']')+2) bracketVar.splice(1,1) bracketVar.splice(bracketVar.length-2,2) bracketVar=translate(bracketVar) //here is where you can translate the value eg [" ","m","o","n","t","h"] to [" ","m","o","i","s"] arr=arr.concat(curlyVar) arr=arr.concat(bracketVar) } parameterisedString=arr.join('') console.log(parameterisedString)

但是,使用您的字符串,布局看起来略有不同.. 正在进行中

 let parameterisedString = '{Group} {Desc} - {termMonth [month]} - {termOdometer [kms]} {DeductibleAmount} {[$] DeductibleType [coverage]}' pm=parameterisedString.split('') var arr=[] let translate=arrOfText=>arrOfText //receives an array of text that would have spacebar then the characters of a term in square bracket.. for example, [" ","m","o","n","t","h"] while(pm.includes('[')&&pm.includes(']')){ var curlyVar=pm.splice(0,pm.indexOf('[')-1) curlyVar.push('}') var bracketVar=pm.splice(pm.indexOf('[')-1,pm.indexOf(']')+2) bracketVar.splice(1,1) bracketVar.splice(bracketVar.length-2,2) bracketVar=translate(bracketVar) //here is where you can translate the value eg [" ","m","o","n","t","h"] to [" ","m","o","i","s"] arr=arr.concat(curlyVar) arr=arr.concat(bracketVar) } parameterisedString=arr.join('') console.log(parameterisedString)

暂无
暂无

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

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