0

How can I do following in Node js/Javascript?

Eg. Say original occurence = "xyz-abc-def" I wanted to replace whole occurrences of above string by "123-abc-456". xyz and def are constant in above string while abc can be any string in original occurence. Eg.

"xyz-asd-def" -> "123-asd-456"
"xyz-ghj-def" -> "123-ghj-456"

How can I do that in node js?

5
  • While asking questions on SO is a real good way to solve problems, i would recommend you to have a deeper regex tutorial first, this is quite a simple problem for regexers Commented Oct 9, 2017 at 18:22
  • 1
    Is it safe to assume "xyz" and "def" are unique across the entire string? Also, how many occurrences would you expect in a single string? Finally, what have you actually tried? Show your work. Commented Oct 9, 2017 at 18:22
  • @James you are right. I actually have no idea on how to do this and I am not sure what to search for this kind of case online also. Actually, I am very new to Node js and javascript. I am writing my first script today. Commented Oct 9, 2017 at 18:26
  • "abc can be any string" does it mean it can include special character? Commented Oct 9, 2017 at 18:39
  • @kgangadhar yes it can. Commented Oct 9, 2017 at 18:42

3 Answers 3

2

You can do this with a regular expression.

var reg = /xyz-(.+?)-def/g;
var input = 'xyz-asd-def';
var result = input.replace(reg, '123-$1-456');
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah you can, but this solution is brittle - try this with "xyz-asd-def xyz-dsa-def", or any other variation of the string where it contains more than one pattern. It might not be important but it's important to explain the limitations of your solution.
@James You're right, in that case the greedy regular expression would fail. This behaviour can be changed easily by making it lazy, and the use-case for this is what decides what way it should behave. I have updated my answer to use a lazy regex, as that seems more suitable for the problem in this case.
0

You can use regular expressions to replace all instances of each text string you're trying to replace.

"def-xyz-asd-def"
    .replace(new RegExp("xyz","g"), "123")
    .replace(new RegExp("def","g"), "456");

Will result in "456-123-asd-456"

Comments

0

Based on the intricacies of your requirements, the best route would be a Regex.

You can use a capture group to extract only the parts of the string you need and, given your patterns are constant, you can rebuild the string e.g.

"xyz-asd-def".replace(/xyz-(\S+)-def/, "123-$1-456");

This will capture any non-whitespace characters inside the pattern of "xyz-xxx-def" with no length restriction. If you expect to have multiple instances of this pattern in a single string, use the global replace option (append g to the end of the regex)

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.