JavaScript String Replace All
Today When I was working I came across the need to replace all the “’” marks with null or with nothing like “” as the “’” mark produces an error while handling with database I have the StringBuldier Class and could replace all at once but to use that it will be a server side code that means irrespective of anything on textchanged event the page should be postback so I decided to use the JavaScript function replace ()
Now the problem with this JavaScript String Replace function is that it replaces the first occurrence in the string. It takes two simple parameters. The first parameter is the pattern to find and the second parameter is the string to replace the pattern with when found. The JavaScript function does not Replace All...
[code]
str=”http://www.msdotnetheaven.com and http://www.msdotnetheaven.com”;
str = str.replace(/heaven/,”mentor”)
[/code]
The output
str=”http://www.msdotnetmentor.com and http://www.msdotnetheaven.com”;
To make the replace function of the JavaScript to replace all the occurrences of the matched string we have to apply a trick and the trick is, use the g modifier like this:
[code]
str=”http://www.msdotnetheaven.com and http://www.msdotnetheaven.com”;
str = str.replace(/heaven/g,”mentor”)
[/code]
The output
str=”http://www.msdotnetmentor.com and http://www.msdotnetmentor.com”;
Subscribe to:
Post Comments (Atom)
Subscribe via email
MSDotnetMentor
MSDotnetMentor
My Website
http://msdotnetmentor.com
Author
Blog Archive
-
▼
2009
(53)
-
▼
March
(14)
- Silverlight 3- Beta - released
- Creating Calender Using Java Script
- Blinking Text Using JavaScript
- Use a session variable in the class files in c# AS...
- ASP page with database handling
- Keycodes at a glance
- JavaScript String Replace All
- Type Text in Upper Case using CSS
- No scrollbar on HTML web pages - JavaScript code
- Transparent or Hide Scrollbar
- disable scroll bar
- Show or Hide combobox at Runtime using JavaScript
- Sending Email Through ASP.NET using C#
- Overview of ASP.Net Framework
-
▼
March
(14)
No comments:
Post a Comment