In this blog post you will learn, the ways you can remove first occurrence of underscore in a string in javascript.
You can use the replace
method in JavaScript to remove the first occurrence of an underscore in a string. The replace
method takes two arguments: the pattern to search for, and the replacement string. To remove the first underscore in a string, you can use the following code:
let str = "hello_world_replace"; str = str.replace("_", ""); console.log(str); // Output: "hello world"
The replace
method searches for the first occurrence of the pattern (in this case, an underscore) and replaces it with the replacement string (in this case, an empty string). If you want to remove all occurrences of the underscore, you can use the g
flag to perform a global search:
let str = "hello_world"; str = str.replace(/_/g, ""); console.log(str); // Output: "helloworld"
You can also use a regular expression to remove the first occurrence of an underscore. For example:
let str = "hello_world"; str = str.replace(/_/, ""); console.log(str); // Output: "hello world"