3

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.

2
  • You only replaced ending !! with a +. 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 Commented Mar 10, 2016 at 20:17

3 Answers 3

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;

Sign up to request clarification or add additional context in comments.

4 Comments

what if i want to match the uppercase word with a given object key and return the value. e.g var mapper={"OH":"oh+","FRIEND":"friend+"};
@gurvk Yes, that should be possible. Though RegExp at solution above matches !! characters, not uppercase letters . Original Question did not include mapping matches to object ?
hey, but it adds a line break after replacing that content. can you help me out from this problem?
@gurvk A line break should not be added to replacement string. Can you create a jsfiddle jsfiddle.net to demonstrate ?
4

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+");

2 Comments

it is working in old case without using object mapper. how it will work with mapper object. i tried using mapper["$1"] instead of ["$1"]
object related problem solved with the help of replacement using function.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.