最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace all matched strings with modified substring - Stack Overflow

programmeradmin1浏览0评论

Actually, I want to replace all matched string used as a key of given object to find the value using javascript regular expression. For example: we have string as

hello,how are you???!!OH!! i am fine. what about you, !!FRIEND!!? I am !!GOOD!!.

and object is like var mapper={"OH":"oh+","FRIEND":"friend+","GOOD":"good+"}

Then output should be like:

hello,how are you???OH+ i am fine. what about you, FRIEND+? I am GOOD+.

As starting and ending !! sign will be replaced with only + sign in the end.

var mapper={"OH":"oh+","FRIEND":"friend+","GOOD":"good+"};
data=data.replace(new RegExp("!![A-Z]*!!", 'g'),modifiedSubstring);

Actually, I want to replace all matched string used as a key of given object to find the value using javascript regular expression. For example: we have string as

hello,how are you???!!OH!! i am fine. what about you, !!FRIEND!!? I am !!GOOD!!.

and object is like var mapper={"OH":"oh+","FRIEND":"friend+","GOOD":"good+"}

Then output should be like:

hello,how are you???OH+ i am fine. what about you, FRIEND+? I am GOOD+.

As starting and ending !! sign will be replaced with only + sign in the end.

var mapper={"OH":"oh+","FRIEND":"friend+","GOOD":"good+"};
data=data.replace(new RegExp("!![A-Z]*!!", 'g'),modifiedSubstring);

I am new to use regular expression but tried some code as placed above. In this expression what should I write instead of modifiedSubstring.

Share Improve this question edited Mar 10, 2016 at 20:58 gurvk asked Mar 10, 2016 at 20:12 gurvkgurvk 2731 silver badge13 bronze badges 2
  • You only replaced ending !! with a +. – user2705585 Commented Mar 10, 2016 at 20:14
  • no, it will match uppercase words starts and end with !!. then replace all of them with + appending in last – gurvk Commented Mar 10, 2016 at 20:17
Add a ment  | 

3 Answers 3

Reset to default 4

Try using RegExp /(\!+(?=[A-Z]+))|(\!+(?=\s|\?|\.|$))/g to match multiple ! characters followed by capital letters , or multiple ! characters followed by space character, . character or end of input. Replace first capture group with "" empty string, replace second capture group with + character

var data = "hello,how are you???!!OH!! i am fine. what about you, "
           + "!!FRIEND!!? I am !!GOOD!!.";

data = data.replace(/(\!+(?=[A-Z]+))|(\!+(?=\s|\?|\.|$))/g
             , function(match, p1, p2) {
                 return p1 ? "" : "+" 
           });

document.body.innerHTML = data;

You can capture the data inside the desired pattern and return it with your desired formatting by using parentheses and $1

    data = "hello,how are you???!!OH!! i am fine. what about you, !!FRIEND!!? I am !!GOOD!!.";

    data=data.replace(new RegExp("!!([A-Z]*)!!", 'g'),"$1+");

You can look for instances like !!word!! and replace it to word+ using this regex.

Regex: !!([A-Z]*?)!!

Explanation:

  • It will match !! followed by a WORD followed by !!.

Replacement to do:

  • \1+ This will replace the match with just WORD+ as WORD falls in first captured group.

Regx101 Demo

发布评论

评论列表(0)

  1. 暂无评论