Forum Moderators: open

Message Too Old, No Replies

need to change ' " ' to ' \" '

want to change a quotation to a \quotation for a form var

         

willis1480

8:42 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



I would like to change the folowing string in a form element:
"hello"

to

\"hello\"

How do i do something like this. currently using regular expressions, but cant figure how to match the quotations.

Bernard Marx

9:29 am on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this demo.
Remember to replace the broken pipe¦ with a proper one.

[pre]
<script type="text/javascript">
function go()
{
var str, elms, reg;
elms = document.getElementsByTagName('input');
str = elms.input.value;
reg = /\'[red]¦[/red]\"/g
elms.output.value = str.replace(reg,"\\$&")
}
</script>

<input type="text" id="input">
<input type="text" id="output">
<button onclick="go()">GO</button>
[/pre]

willis1480

1:48 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



thanks, I will try this. The regular expressions has been difficult for me. What did you mean "replace broken pipe with correct one"? Do you mean with the character I would like replaced?

Bernard Marx

3:21 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll explain in HTML:

<p style="font-size:2em">
I mean that, when you copy & paste the code,
you should then replace<br>
this character: &#166;<br>
with this one: &#124;<br>
before you run the code.
</p>

willis1480

3:23 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



i got it thanks to your regular expression, but i could not get the rest of the code to work. This is what I ended up with:

function process(){
for(index = 0; index < document.form1.length; index++){
if(document.form1[index].value){
str = document.form1[index].value;
reg = /\'¦\"/g;
document.form1[index].value = str.replace(reg,"\\$&")
}
}
}

Bernard Marx

6:02 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<update>
Ignore that below.

It's because you aren't changeing the broken ¦ pipe in the code!

-------------------------
Yeah. Strange one that.
Sure there's a decent explanation (just don't know what it is).
I had to work around it by using unicode char codes for the quote chars into the RegExp:

[pre]
<script>
function process(form)
{
var elm, reg = /\u0027¦\u0022/g;
for(index = 0; index < form.length; index++)
{
elm = form[index]
if(elm.value){
elm.value = elm.value.replace(reg,"\\$&")
}
}
}
</script>

<form name="form1">
<input type="text">
<input type="text">
<input type="text">
<button onclick="process(this.form)">PROCESS</button>
</form>
[/pre]

willis1480

3:20 pm on Nov 26, 2004 (gmt 0)

10+ Year Member



no, i did change the broken pipe. Either way, got it working and thank you very much for your help.