how to replace with (/) forward slash

Posted by Abhi_patil under JavaScript on 3/2/2009 | Views : 82204 | Status : [Member] | Replies : 13
i want to know how to use forward slash in javascript.
i want to replace all occurances of forward slash(/) with splace (' ') in javascript.




Responses

Posted by: Elizas on: 2/9/2010 [Member] Starter

Up
0
Down
In replace function you can see that we are passing the regular expression to replace, but if the data is dynamic and i want to replace it in runtime ?? i have to store the data in a variable and now comes the question how i can use a variable in replace function ????

suppose you are getting the value from the textbox called subject


var subject = document.getElementById('subject').value;
var main_str = "i am working as a php developer, php is favorite language ";

//i want to replace the subject variable - but as a rule in replace function we have to pass a regular exprssion
// so we cannot write main_str = main_str.replace(subject ,'JAVA'); we have to convert the subject to a regular expression

var sRegExInput = new RegExp(subject, "g");
main_str = main_str.replace(sRegExInput ,'JAVA');

http://www.mindfiresolutions.com/How-to-use-a-variable-in-replace-function-of-JavaScript-676.php

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kobusp on: 2/9/2011 [Member] Starter | Points: 25

Up
0
Down
You have to escape the "/" like this (no need for a recursive function):
"2011/02/02".replace(/\//g," ");

Gives "2011 02 02"

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Check this link

http://www.w3schools.com/jsref/jsref_replace.asp

http://www.tizag.com/javascriptT/javascript-string-replace.php

Thanks
Karthik
www.f5Debug.net

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 2/9/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
something similar to String.Replace function.

Thanks

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Javascript doesnt have string.replace directly.... you need to mention as its shown in the above examples

Thanks
Karthik
www.f5Debug.net

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/10/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi

try this way in javascript

var str = "Madhu/Babu";
alert(str);
var str1 = str.replace('/',' ');
alert(str1);

Regards
Madhu


Thanks and Regards
Madhu

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kobusp on: 2/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthik,

In JavaScript a quoted string is an instance of the String object. This means any of the following is possible:
"Hello".length;

"Hello".match(/l/g);
"Hello".replace(/lo/g,"p");
"Hello".concat(" Karthik");

Give it a try.

Regards,
Kobus


Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Weirdo on: 5/6/2011 [Member] Starter | Points: 25

Up
0
Down
In order to replace all occurrences you need global regular expression for that use "g" identifier following the regular expression. for details
http://www.inditektalk.com/article/how-to-replace-all-occurrences-substring-string-using-javascript-replace-function

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srinivas673 on: 5/6/2011 [Member] Starter | Points: 25

Up
0
Down

here i am using - replace by /
replace(/-/g, "/"));
same way
replace(///g, " "));

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja on: 3/2/2009 [Member] Starter

Up
0
Down
Please see following links for solution

http://vijaymodi.wordpress.com/2007/12/25/replace-forward-slashes-with-backward-slashes/

http://www.webdeveloper.com/forum/showthread.php?t=164181

Thanks.


Regards,
Raja, USA

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 3/2/2009 [Member] Bronze

Up
0
Down
It's simple you have to put this JavaScript

<script language="javascript" type="text/javascript">
function change()
{
var test = 'HIiiiiiiiii/fdfd';
alert(test.replace('/', ' '));
}
</script>


Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi_patil on: 3/3/2009 [Member] Starter

Up
0
Down
Hi Neeks
this replaces only first occurrances.
in javascript to remove all occu. u can use
/text to remove/g
but problem is in forward slash
when u use this i.e ///g
then this becomes comment.

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 3/4/2009 [Member] Bronze

Up
0
Down
Replace the JavaScript with the Following
<script language="javascript" type="text/javascript">

var test = "HIi/iiiiiiii/fd//fd";
function change()
{
test = test.replace("/", " ");
if(test.indexOf("/") != -1)
change();
}
function change1()
{
change();
alert(test);
}
</script>


call the Function change1()

Abhi_patil, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response