Saturday 28 June 2014

String Replace and Search count using jquery/Javascript

Jquery or Javascript's Replace function does not provide facility to replace all occurrence of the search string it only search 1st occurrence and replace with other. To solve that issue, I have created function to find each occurrence All Replace from string . You can see in below image the function name solidReplace(Main string, Search string, Replace string).

Other function is the count. If you want to count the number of occurrence from the string. You can use function getCount() which is displaying in the image below. Syntax is (main string, search string).

 



Friday 27 June 2014

Convert Newline (\n) to BR tag using javascript/jquery

To maintain textarea's format in the database and display time, we must have to use javascript or jquery's function to detect Enter key of the text area. When user press enter key, text area consider it as the /n instead of the <br> tag. But while displaying it into the HTML, it only understand the <br> tag and remove the /n so format will look like below image's top section. After impelementing the NL to BR replace, it will appear like bottom part of the image.

JS Fiddle Direct Link 

This function remove the NL (\n) and replace it with the BR tag.

function replaceNLtoBR(p_str) 
{
     return p_str.replace(/(\r\n|\n|\r)/gm,"<br>");
--------------------------------------------------------
You can also replace BR with NL(\n)

function replaceBRtoNL(p_str) 
{
     var regex = /<br\s*[\/]?>/gi; 
     return p_str.replace(regex,"\n");
}
---------------------------------------------------------
Check this live on the fiddle : http://jsfiddle.net/mikegadani/QjNE6/

www.techplussoftware.com