To replace substring.But not working for me...
var str='------check';
str.replace('-','');
Output: -----check
Jquery removes first '-' from my text. I need to remove all hypens from my text. My expected output is 'check'
JavaScript replace() Method: We can replace a portion of String by using replace() method. JavaScript has an inbuilt method called replace which allows you to replace a part of a string with another string or regular expression. However, the original string will remain the same.
The JavaScript String replace() method returns a new string with a substring ( substr ) replaced by a new one ( newSubstr ). Note that the replace() method doesn't change the original string. It returns a new string.
Algorithm to Replace a substring in a stringInput the full string (s1). Input the substring from the full string (s2). Input the string to be replaced with the substring (s3). Find the substring from the full string and replace the new substring with the old substring (Find s2 from s1 and replace s1 by s3).
To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.
simplest:
str = str.replace(/-/g, "");
replace
only replace the first occurrence of the substring.
Use replaceAll
to replace all the occurrence.
var str='------check';
str.replaceAll('-','');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With