Ali Imran
04-02-2003, 02:39 PM
What is the function in javascript and how to use it which will work as function str_replace of php ?
regards
Ali Imran
regards
Ali Imran
|
Click to See Complete Forum and Search --> : what is function in javascript (equvilant to str_replace in php) ? Ali Imran 04-02-2003, 02:39 PM What is the function in javascript and how to use it which will work as function str_replace of php ? regards Ali Imran Jona 04-02-2003, 02:44 PM Okay... Let me think.... You're trying to replace a string of text with another string? Ok... document.formName.textboxName.value=document.formName.textboxName.value.replace("string", "newString"); Ali Imran 04-02-2003, 02:46 PM And for example I want to replace \n characters on a specific position of string not in entire string what am I going to do thanks for reply. Jona 04-02-2003, 02:52 PM var a = document.myForm.myTextField.value; a=a.split("\\n").join("replace with your text"); Now all of the \n's will be replaced with, "replace with your text" You can also use Regular Expressions to do this. cgraz 04-02-2003, 02:56 PM I'm new to javascript myself, but if you want a function similar to PHP's nl2br(), this will work for you.<script lanaguage="javascript"> var myString = "this is a string\n that needs \n\n to be on a newline"; var myString2 = myString.replace(/\n/g, "<br>"); document.write (myString2); </script> nl2br() specifically replaces newlines (\n) with html breaks (<br>). If you wanted to replace a newline with something else, just change the <br> to whatever you wanted to replace it with. this is different from str_replace because you can use regular expressions within it, where in PHP you'd have to use preg_replace or ereg_replace or something of that sort. Cgraz Ali Imran 04-02-2003, 02:57 PM i want to replace the ones on specific postion only like for example str="this \nis mt \ntext\na1\na2sdflks\ndjfsdj"; now i want to replace only the ones between and is there any solution ? Jona 04-02-2003, 03:00 PM Yes, use .substring(). Ali Imran 04-02-2003, 03:02 PM can you give an example please. Jona 04-02-2003, 03:10 PM Yah, sure... Hold on a minute... Let me set up the code. Jona 04-02-2003, 03:20 PM <html><head> <script> function Change(){ elems = document.frm; var n = document.frm.txt2.value.indexOf(""); var m = document.frm.txt2.value.indexOf(""); if(n== -1) { n = document.frm.txt2.value.length;} var nq = document.frm.txt2.value.substring(n, m); nq=nq.split("[x]").join(""); if(nq){ nq=nq.split("\\n").join("<br>"); } else { return false; } alert(nq); } </script> </head><body> <form name="frm"> <input type=text name="txt2"> <input type=button onclick="Change()" value="Okay"><br> </form></body></html> Ali Imran 04-02-2003, 03:22 PM thank you so much for your quick and accurate help.. I really appreciate this... thanks again... Jona 04-02-2003, 03:23 PM It was your idea, so I thank you. :P cgraz 04-02-2003, 03:25 PM How would you use a regular expression to do the replace? Wouldn't that be easier? Something that it says change any \n found to the right of but to the left of to a <br> Cgraz Jona 04-02-2003, 03:27 PM That would be like this: nq=nq.replace(/\\n/g, "<br>"); I think... :D cgraz 04-02-2003, 03:29 PM isn't that essentially the same as my regular expression above? I think that the reg exp you wrote (as well as mine) replaces all newline characters, not just the ones found betwen and Cgraz Jona 04-02-2003, 03:33 PM I just tested it. It works fine. It only extracts the ones inside the and . cgraz 04-02-2003, 03:36 PM how does that work? You make no mention of or in your reg exp. I tried<script lanaguage="javascript"> var str = "this \nis mt \ntext\na1\na2sdflks\ndjfsdj"; var myString2 = str.replace(/\\n/g, "<br>"); document.write (myString2); </script>And it didn't do anyting. I then changed /\\n/g to /\n/g and it replaced all the newlines in the string, but not just the ones betwen and Cgraz Jona 04-02-2003, 03:44 PM But you see what I did? <html><head> <script> function Change(){ elems = document.frm; var n = document.frm.txt2.value.indexOf(""); //search for string, "" var m = document.frm.txt2.value.indexOf(""); //search for string, "" if(n== -1) { n = document.frm.txt2.value.length;} var nq = document.frm.txt2.value.substring(n, m); //set a variable to get what is in between variables n and m nq=nq.split("[x]").join(""); //replace all [x]'s with nothing (get rid of them) if(nq){ //if nq has anything in it nq=nq.replace(/\\n/g, "<br>"); } /*make nq equal itself, but with all \n's replaced with <br>. Notice that this is after I defined nq to equal the substring of the variables n and m*/ else { return false; } //if nq has nothing in it, just return false alert(nq); //alert what nq contains } </script> </head><body> <form name="frm"> <input type=text name="txt2"> <input type=button onclick="Change()" value="Okay"><br> </form></body></html> cgraz 04-02-2003, 03:48 PM oh i didn't know you meant in that code. But I mean just a one line regular expression to replace newlines between and . (without the having to split the string up using other methods). It has to be possible, right? Someting like:<script lanaguage="javascript"> var str = "this \nis mt \ntext\na1\na2sdflks\ndjfsdj"; var myString2 = str.replace(reg exp here, "<br>"); document.write (myString2); </script> Cgraz Jona 04-02-2003, 03:55 PM Not exactly: <script lanaguage="javascript"> elems = document.frm; var str1 = document.frm.txt2.value.indexOf(""); var str2 = document.frm.txt2.value.indexOf(""); var myString2 = document.frm.txt2.value.substring(str1, str2); myString2=myString2.split("").join(""); myString2=myString2.replace(/\\n/g, "<br>"); document.write(myString2); </script> What we're doing is first searching for the [x] and , and set myString2 to get everything in between and . Then we use the spli() method to get rid of the first [x]. Then we replace all of what we're searching for (\n) to <br>. cgraz 04-02-2003, 04:14 PM Ok I see. I could've sworn there would've been a way to use a regular expression on that. This is going to bug me for a while. I'll try to play around with it when I get more familiar with javascript. I did get a regular expression that replaced just the newline after , but it didn't replace what was between [x] and Anyways, thanks for the clarification. Cgraz Ali Imran 04-03-2003, 02:31 PM Thank you so much for your kind help guys..... Actually I am new to this script and dont know objects' properties and functions fully. Please check the attachment what I wanted to do was actually what I could not express. I wanted to convert selected lines of text to BBCode list format and I did it with you kind help. It creates a BBCode for list of selected text. Enter some lines of text and seperate it on newlines select the desired ones and them press okay button. I am going to use it in a new forum software I am creating with EDB which is a PHP based database system which does not require any other support than just php. I created it a month ago, and rightnow its beta testing is going on. anyhow thanks again Please check the attachment regards Ali Irman sory the attachment was not possible so i paste code here save it as html and check it pelase. <html><head> <script> function createlist(){ form1.tp.focus(); var selected = document.selection; var srange = selected.createRange(); srange.colapse; nq=srange.text.split("\n").join("\n "); srange.text="\n " + nq +"\n"; } </script> </head><body> <form name="form1"> <textarea type=text name="tp" rows=10></textarea> <input type=button onclick="createlist()" value="Okay"><br> </form></body></html> Ali Imran 04-03-2003, 02:33 PM code is not being shown properly in post now check attachment Jona 04-03-2003, 02:40 PM Does the script not work? What effect are you trying to get? You want the ['*] to only make a bullet if inside of the ['list] and [/'list] tags? Ali Imran 04-03-2003, 02:46 PM What I wanted is done. I wanted multiple selected lines be convertd to BBCode list easily and that is successfully done by your help. thanks.. Jona 04-03-2003, 02:58 PM Oh, you're very welcome! :D aapun 04-03-2003, 03:06 PM I see some real reg ex GURUS out here. Can you guys help me solve my problem? http://forums.webdeveloper.com/showthread.php?s=&threadid=7148 here is my question again : I need to break a string in a particular pattern, and store the broken pieces into an array: (two chars), (two chars), ... . . . . (three chars), (two chars) Please suggest the simplest way to do this. While loop and a sequence of "if" statements is the obvious solution. Can any one make use of "split" or "regular expression" some how and solve it ? example1: 1293878334 output: (1)(29)(38)(783)(34) example2: 12334 output: (123)(34) example3: 334 output: (3)(34) So basically, how do i specify the number of characters in my reg ex? any kinda help appreciated. el_timm 08-14-2006, 04:25 PM To Expand on on cgraz, you can prototype the String object: String.prototype.nl2br = function() { return this.replace(/\n/g, "<br />"); } // Usage formattedStr = unformattedStr.nl2br(); webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |