简体   繁体   中英

how to replace string with another string in javascript

I am trying to replace string with another string.

var string = "equity,40\n\
            equity-cap,15\n\
            cash,20\n";

var output = string.replace(name, `${name},${value}`);

//output 
output = "equity,40\n\
            equity-cap,15\n\
            cash,80,20\n"

var string = "equity,40\n\
            equity-cap,15\n\
            cash,20\n";

var text = cash
var value = 80

 var string = "equity,40\\n\\ equity-cap,15\\n\\ cash,20\\n"; var name = "cash"; var value = 80; var output = string.replace(name, `${name},${value}`); //output output = "equity,40\\n\\ equity-cap,15\\n\\ cash,80\\n" console.log(output, "required output") 

var output = "equity,40\n\
            equity-cap,15\n\
            cash,80\n";

The clue is that you're not trying to replace the value as well in your code, so you end up with both the old value and the new one.

If you know that the old value is 20 , it's very easy and we just have to add that to the string we'll replace.

If you don't know that the old value is 20 , it becomes a bit more difficult since we have to search for that value.

We can do that by using a regular expression, which can also detect numbers, whatever the number is.

If the source data comes from a CSV file ( which I assume due to the string structure ), we can also try to leverage how CVS is structured to parse the string into an actual object. The big advantage is that we do not have to know any values ( 20 in this case ) and that you can change any value directly by name through object property access.

PS: there's several other ways to tackle this problems, so use what makes the most sense to the project.

 // Option 1: // If we know that the old value is 20: var str_source = "equity,40\\n\\equity-cap,15\\n\\cash,20\\n"; var name_to_replace = "cash"; var value_to_replace = 20; var replacement_name = "cash"; var replacement_value = 80 var output = str_source.replace( `${ name_to_replace },${ value_to_replace }`, `${ replacement_name },${ replacement_value }`); console.log( 'option 1:\\n', output, '\\n' ); // Option 2: // If we do not know what the old value is. // I think this string comes from a CSV file? // So we can assume there's always either a \\n character or end-of-line character after the number. // We can create a Regular Expression that will look for the digits and the end of line. var str_source = "equity,40\\n\\equity-cap,15\\n\\cash,20\\n"; var name_to_replace = "cash"; var replacement_value = 80; // Detect: // the name_to_replace // the comma following the name // all digits following the comma // one valid whitespace charater OR the end of the string var regexp = new RegExp( `${ name_to_replace },\\\\d+?\\\\s|$` ); var output = str_source.replace( regexp, `${ name_to_replace },${ replacement_value }\\n`); console.log( 'option 2:\\n', output, '\\n' ); // Option 3: // If this data really comes from a CSV file, we might as well just turn it into an object, so we don't have to manually search for specific values. // We can just leverage splitting on commas and new lines to create // an object we can edit directly by name. var str_source = "equity,40\\n\\equity-cap,15\\n\\cash,20\\n"; var name_to_replace = "cash"; var replacement_value = 80; var combinations = str_source.trim().split( '\\n' ); // Create the object so we get: // { equity: "40", equity-cap: "15", cash: "20" } var obj_source = combinations.reduce(( obj, combo ) => { var chunks = combo.split( ',' ); obj[ chunks[0] ] = chunks[1]; return obj }, {}); // we can now just replace the value directly in the object: obj_source[ name_to_replace ] = replacement_value; // recreate the string from the object: var output = Object .entries( obj_source ) .map(([ key, value ]) => `${ key },${ value }` ) .join( '\\n' ); console.log( 'option 3:\\n', output, '\\n' ); 

Based on your question, It seems to be you want to find such part of a string and replace it with the new one, If it's feet to your requirement then below code will help you.

  const str = "Hello World, It's a hello from the developer"; const name = 'Hello'; const value = 'Hi'; const replaceString = (string, search, replaceWith) => { return string.split(search).join(replaceWith); }; console.log(replaceString(str, name, value)); console.log(replaceString('Hello...hello..Hello', name, value)); 

Note It's case-sensitve

You can replace a part of another string using replace() . I hope this helps.

 var string = "equity,40\\n\\ equity-cap,15\\n\\ cash,20\\n"; var name = 20; var value = 80; // string.replace("string which has to be changed", "string which is going to replace the previous string") var output = string.replace(name, value); console.log(output, "required output") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM