0

I have a string

Date    Id    Number    Owner   GenderMaleFemale    Employment TypeExperiencedFresher   Issue TypeAutomation Code (script typos, File issues) Code (data model errors, utility errors)Platform Issues(db start up, network)Selectnull   

I want to replace GenderMaleFemale with Gender, Employment TypeExperiencedFresher with Employment Type and replace Issue TypeAutomation Code (script typos, File issues) Code (data model errors, utility errors)Platform Issues(db start up, network)Selectnull with Issue Type in the string itself.

The values of Issue Type are never constant. I want this change to happen dynamically. How can I achieve this?

EDIT: I tried doing

var string = "Date  Id    Number    Owner   GenderMaleFemale    Employment TypeExperiencedFresher   Issue TypeAutomation Code (script typos, File issues) Code (data model errors, utility errors)Platform Issues(db start up, network)Selectnull   "
string = string.replace("GenderMaleFemale", "Gender") 
console.log(string) // It replaces the GenderMaleFemale string as Gender. 
// But I don't know the value after Gender usually. Right now the options added are Male, Female if a new option gets added then I need to change the code. So I want replacement to be dynamic.
// I want to achieve something like
string = string.replace(/Gender/g, "Gender") // The output should be GenderMaleFemale word replaced as Gender.
4
  • Can you make a start on this, so readers know where to start helping you? This is a bit vague at the moment. It sounds like you need String.replace. Commented Apr 8, 2021 at 10:08
  • @halfer Thanks for the response, I m not getting how to replace the word containing GenderMaleFemale with Gender. For String.replace, if I pass Gender and replace the same as Gender no difference is seen. How can I achieve it? Commented Apr 8, 2021 at 10:12
  • replace will return the new string, it won't mutate the original string. Refer the MDN doc page mentioned for examples Commented Apr 8, 2021 at 10:14
  • It sounds like you have some code. Great! Please edit it into your question. Use the block formatting tool to preserve the indentation. Commented Apr 8, 2021 at 10:21

2 Answers 2

1

Using regex is the right idea. By defining a regex which looks for the characters Gender, followed by all non-whitespace characters you can identify any form of GenderX until a whitespace is met.

string = string.replace(/(\s)Gender[^\s]+/, '$1Gender');

I also included a whitespace character in front of Gender that is preserved in the replacement ($1).

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

Comments

0

Replace Methid will return the new string so you have to init it to its self.

Try this :-

var WholeString =  'Date    Id    Number    Owner   GenderMaleFemale    Employment TypeExperiencedFresher   Issue TypeAutomation Code (script typos, File issues) Code (data model errors, utility errors)Platform Issues(db start up, network)Selectnull   ';

WholeString = WholeString.replace('GenderMaleFemale','Gender').replace('Employment TypeExperiencedFresher','Employment Type').replace('Issue TypeAutomation Code (script typos, File issues) Code (data model errors, utility errors)Platform Issues(db start up, network)Selectnull','Issue Type');

console.log(WholeString)

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.