DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

replace \

This is a discussion on replace \ within the JavaScript Programming forums, part of the Programming & Scripting category; I have a JS function that seems to be working fine until a value is added to a hidden text ...

Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

  #1  
Old May 7th, 2009, 09:52 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default replace \

I have a JS function that seems to be working fine until a value is added to a hidden text box. The function causes no problems until a value is added to the txt_filepath textbox, which has visibility:hidden style added to it.

I think the problem is linked to the fact that the value in the textbox is in the form of \1\randomletters. I think it's the \'s that are causing the problem. How would I do a replace on them in JS?

The error I get in Firefox is AddTask is not defined.

The JS function is as follows:-
Code:
function AddTask(){
var tr_box = document.getElementById('tr_box_start1');
//alert(tr_box);
if(tr_box!=null){
document.getElementById('tr_box_start1').style.display='none';
document.getElementById('tr_box_start2').style.display='none';
}
document.getElementById('ctl00_main_content_update_progress').style.display='';
document.getElementById('update_text').innerHTML='Adding Task....';
var pcuser = document.getElementById('ctl00_hdn_pcuser').value;
var defaulttask = document.getElementById('ctl00_main_content_ddl_defaulttask').value;
if(defaulttask=='') defaulttask=0;
var visitorref = document.getElementById('ctl00_main_content_ddl_visitorref').value;
if(visitorref=='') visitorref=0;
var introref = document.getElementById('ctl00_main_content_ddl_introref').value;
if(introref=='') introref=0;
var cat = document.getElementById('ctl00_main_content_ddl_category').value;
var sendto = document.getElementById('ctl00_main_content_ddl_sendto').value;
var dtDue = document.getElementById('ctl00_main_content_txt_datedue').value;
var tmSpent = document.getElementById('ctl00_main_content_txt_timespent').value;
var title = document.getElementById('ctl00_main_content_txt_subject').value;
//alert(title);
var filepath = document.getElementById('ctl00_main_content_txt_filepath').value;
filepath=escape(filepath);
alert('filepath: '+filepath);
var note=FTB_API['ctl00_main_content_ftb_mess'].GetHtml();
//alert(note);
//return false;
TaskAdd.AddTask(pcuser,defaulttask,visitorref,introref,cat,sendto,dtDue,tmSpent,title,filepath,note,OnAddTaskComplete,OnError,OnTimeout);
}
Any ideas?
__________________
Join the folding team
Reply With Quote
  #2  
Old May 7th, 2009, 09:57 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

The backslash is an escape character in JS, so that could possible be causing the problem.

Here's how to replace in JS: JavaScript replace() Method

To keep the backslash in the string, simply replace it with two backslashes.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #3  
Old May 7th, 2009, 10:06 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

That's what I think J, but the problem is getting the syntax right on the replace function.
This gives an error of unterminated regular expression literal
Code:
document.write(str.replace(/\/,"\\"));
If I try this:-
Code:
var str="Visit \Microsoft!";
document.write(str.replace(/\\/,\\\\)
It just returns "Visit Microsoft!" when I would expect it to return "Visit \\Microsoft!"
Reply With Quote
  #4  
Old May 7th, 2009, 10:14 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Try this:
Code:
document.write(str.replace(/\\/g, "\\\\");
Reply With Quote
  #5  
Old May 7th, 2009, 10:24 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Nope. It doesn't work if the original string just has a single \ in it.

So, this:-
Code:
var str="Visit \\1\\Microsoft!";
document.write(str)
gives Visit \1\Microsoft!

If I do:-
Code:
var str="Visit \\1\\Microsoft!";
document.write(str.replace(/\\/g,"\\\\"));
it gives Visit \\1\\Microsoft!

If it starts with:-
Code:
var str="Visit \1\Microsoft!"
then it doesn't do a replace.

Any other ideas?
Reply With Quote
  #6  
Old May 7th, 2009, 10:32 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

What about this:
Code:
document.write(str.replace('\\', '\\\\');
Reply With Quote
  #7  
Old May 7th, 2009, 10:36 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Nope. Still doesn't work.

There must be a way of doing this, surely?

If the original string just has a single \ then it just gets rid of it.

This is very annoying..
Reply With Quote
  #8  
Old May 7th, 2009, 11:11 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

From what I've found it seems it's not possible to do this with Javascript.

The original string must have double slashes to do a replace.
Reply With Quote
  #9  
Old May 7th, 2009, 11:15 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

I think I'm getting closer, R...but still getting weird results:
Code:
<script type="text/javascript"><!--

var esc = {"\\":/\\/g, n: /\n/g, t: /\t/g, r: /\r/g, b: /\x08/g, s: /\x73/g}
    var Str= "\1\randomletters";
var newStr = Str;
for(e in esc){
  newStr = newStr.replace(esc[e], "\\"+e);
}
    alert(newStr);
//-->
</script>
Reply With Quote
  #10  
Old May 7th, 2009, 11:26 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Hey R,

Maybe if you convert to forward slashes first:
Code:
        function back2forward(dataStr) {
            return dataStr.replace(/\\/g, "/");
        }
Then, handle the string how you need to...then when you're done, convert it back:
Code:
        function forward2back(dataStr) {
            return dataStr.replace(/\//g, "\\");
        }
I'm running out of ideas on this one..
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Bookmarks

Tags
javascript, replace

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:20 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2