replace()
methodYou can use the JavaScript replace()
method to replace the occurrence of any character in a string. However, the replace()
will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global (g
) modifier. The following example will show you how to replace all underscore (_
) character in a string with hyphen (-
).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Replace Character in a String</title>
<script>
function strReplace(){
var myStr = 'quick_brown_fox';
var newStr = myStr.replace(/_/g, "-");
// Insert modified string in paragraph
document.getElementById("myText").innerHTML = newStr;
}
</script>
</head>
<body>
<p id="myText">quick_brown_fox</p>
<button type="button" onclick="strReplace();">Replace</button>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to replace character inside a string in JavaScript
Use the JavaScript replace() method Y...Restful API In Laravel 5.5 Using jwt Authentication
Today, we are share with you how to buil...How to Parse JSON in JavaScript
Use the JSON.parse() Method You...Excel and csv import export using maatwebsite in laravel example
We are share with you in this tutorials...How to find string length in PHP
Use the PHP strlen() function...