- To remove white space in a string in JavaScript at the starting and ending. i.e both sides of a string we have trim() method in JavaScript.
- By Using .trim() method we can remove white spaces at both sides of a string.
- var str = " instanceofjava.com ";
alert(str.trim()); - Will alert a popup with instanceofjava.com.
- If we want to remove last character of a string then we need to use replace method in JavaScript.
- str = str.replace(/,\s*$/, "");
- We can also use slice method to remove last character.
Remove specific characters from a string in Javascript
- To remove specific character from string in java script we can use
- var string = 'www.Instanceofjava.com';
- string.replace(/^www.+/i, ''); 'Instanceofjava.com'
No comments