Javascript RegEx匹配字符后的字

时间:2021-10-14 20:46:36

I have been looking for hours trying to figure this one out, perhaps I can sleep on it. Im trying to RegEx part of a string and replace it with a value depending on the word after a character.

我一直在寻找好几个小时试图解决这个问题,或许我可以睡觉了。我试图RegEx部分字符串,并将其替换为取决于字符后的单词的值。

"content.size | price: $content.price" What I am trying to do is use a RegEx to replace parts of my string with a value. I would like to target the entire expression content.size and content.price. But the word after "content." can be anything. I have tried to write an expression but not having any luck

“content.size | price:$ content.price”我想要做的是使用RegEx用一个值替换我的字符串部分。我想定位整个表达式content.size和content.price。但是“内容”之后的这个词。可以是任何东西。我试着写一个表达但没有运气

var firstString = "content.size | price: $content.price"
var Re = new RegExp(/content.\b/);
var newValue = firstString.match(Re);
console.log(newValue);

enter code hereWhat I am expecting from the console is:

在这里输入代码我期望从控制台获得的是:

content.size content.price

So then I can say:

那么我可以说:

firstString.replace(newValue, someOtherValue)

2 个解决方案

#1


0  

One problem with your regex is that you are not escaping the decimal point (.) which matches any single character except the newline - documentation is here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

你的正则表达式的一个问题是你没有转义小数点(。),它匹配除换行符之外的任何单个字符 - 文档在这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/常用表达

I have tried the below which returns an array of content.size and content.price

我尝试了下面的内容,它返回一个content.size和content.price数组

You can try it out and see if it helps you:

您可以尝试一下,看看它是否对您有所帮助:

var firstString = "content.size | price: $content.price";
var regex = /content\.[\S]+/g;
var newValue = firstString.match(regex);
console.log(newValue);

Let me know if it helps you.

如果它对您有所帮助,请告诉我。

SO1

SO1

#2


1  

You could use a replacer function, although I'm not 100% sure I got the question right

你可以使用替换器功能,虽然我不是100%确定我的问题是正确的

var firstString = "content.size | price: $content.price"
var regex = new RegExp(/content\.([^ ]*)/g);
var newValue = firstString.replace(regex,function(match, group1){
    if(group1 === 'size'){
        // do something with size here
        return '12';
    }
    // return key if not handled
    return group1;
});
console.log(newValue);

Update:

更新:

As it seems that the question was how to dynamically update size and price here is one more complete example.

似乎问题是如何动态更新大小和价格这里是一个更完整的例子。

var data = {size : "12", price : "$ 15.99"};
var firstString = "$content.size | price: $content.price"
var regex = new RegExp(/\$content\.([^ ]*)/g);
var newValue = firstString.replace(regex,function(match, group1){
    if(group1 in data){
        return data[group1];
    }
    // return key if no data present
    return group1;
});
console.log(newValue);

I hope this makes it more clear.

我希望这更清楚。

#1


0  

One problem with your regex is that you are not escaping the decimal point (.) which matches any single character except the newline - documentation is here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

你的正则表达式的一个问题是你没有转义小数点(。),它匹配除换行符之外的任何单个字符 - 文档在这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/常用表达

I have tried the below which returns an array of content.size and content.price

我尝试了下面的内容,它返回一个content.size和content.price数组

You can try it out and see if it helps you:

您可以尝试一下,看看它是否对您有所帮助:

var firstString = "content.size | price: $content.price";
var regex = /content\.[\S]+/g;
var newValue = firstString.match(regex);
console.log(newValue);

Let me know if it helps you.

如果它对您有所帮助,请告诉我。

SO1

SO1

#2


1  

You could use a replacer function, although I'm not 100% sure I got the question right

你可以使用替换器功能,虽然我不是100%确定我的问题是正确的

var firstString = "content.size | price: $content.price"
var regex = new RegExp(/content\.([^ ]*)/g);
var newValue = firstString.replace(regex,function(match, group1){
    if(group1 === 'size'){
        // do something with size here
        return '12';
    }
    // return key if not handled
    return group1;
});
console.log(newValue);

Update:

更新:

As it seems that the question was how to dynamically update size and price here is one more complete example.

似乎问题是如何动态更新大小和价格这里是一个更完整的例子。

var data = {size : "12", price : "$ 15.99"};
var firstString = "$content.size | price: $content.price"
var regex = new RegExp(/\$content\.([^ ]*)/g);
var newValue = firstString.replace(regex,function(match, group1){
    if(group1 in data){
        return data[group1];
    }
    // return key if no data present
    return group1;
});
console.log(newValue);

I hope this makes it more clear.

我希望这更清楚。