Boy…
This has been my last four posts :rolleyes:… I guess over time I’ll get better with regular expressions.
My issue is that I’m trying to catch single quotes in a form field and replace it with something else. I think I’m close, but I know (as always) my reg exp is a little off…
Here’s what I have:
[CODE]
function replacecomma(){
var fields=document.user_form.elements;
var target_pattern;
var main_fields=new Array();
var i=0;
while(i<fields.length){
if (fields[i].name==”text_field”)
{main_fields.push(fields[i]); i++;}
}
for (var i=0; i<main_fields.length; i++){
var user_entry=main_fields[i].value;
for (var j=0; j<user_entry.length; j++){
if (user_entry.charAt(j)==”‘”){
[COLOR=”Blue”] var x=user_entry.charAt(j);
var pattern=eval(‘/^(((.)(‘{0})){‘+x+’}((.)(‘{0})))/g’);
user_entry=user_entry.replace(pattern, “somethingelse”);
main_fields[i].value=user_entry;[/COLOR]
}
}
}
}
Can anyone get me closer to a solution?
[code=html]
function rplc(s,o,n){ //(string,old,new)
while(s.indexOf(o)!=-1){s = s.replace(o,n);}
alert(s); // return s
}[/code]
[code=html]
function ReplaceStuff(S,O,N){ var rx=new RegExp(O,'g'); S=S.replace(rx, N);
alert(S); // return S
}[/code]
Here's what I have, only need to tweek it a bit, perhaps not
One way to do it...
[code=html]
function rplc(s,o,n){ //(string,old,new)
while(s.indexOf(o)!=-1){s = s.replace(o,n);}
alert(s); // return s
}[/code]
Another way...
[code=html]
[/QUOTE]
function ReplaceStuff(S,O,N){ var rx=new RegExp(O,'g'); S=S.replace(rx, N);
alert(S); // return S
}[/code]
Those codes will do it but, you only wish to make it happen on the first instance of a single quote?[/QUOTE]
[CODE]
var ms = "my original string";
var pattern1 = /original/;
// instead of pattern in the line below you can also write the expression itself: /original/
var ms1 = ms.replace( pattern1, "our" );
ms1; // -> "ourstring";
// when you use the 'g' flag behind the end slash of the regexp (you just write the letter g behind it), not only the first, but all matches are found:
var pattern2 = /in/g; // finds all "in"s;
var ms2 = ms.replace( pattern2, "IN"); // "my origINal strINg"
[/CODE]
[CODE]
var mystring = "there was a so called 'party' at angela's house";
var re1 = mystring.match(/'/);
mystring1 = mystring.replace( re1,'"');
mystring1; // -> "there was a so called "party' at angela's house";
[/CODE]
[CODE]
var mystring = "there was a so called 'party' at angela's house";
var re2 = /'/g;
mystring2 = mystring.replace( re2,'"');
mystring2; // -> "there was a so called "party" at angela"s house"
[/CODE]
[CODE]
var mystring = "there was a so called 'party' at angela's house";
var re3 = /B'|'B/g;
mystring3 = mystring.replace( re3,'"');
mystring3; // -> "there was a so called "party" at angela's house"
[/CODE]
[CODE]
for (var i=0; i<main_fields.length; i++){
var user_entry = main_fields[i].value;
// you can use one of the 2 lines below in your replace method:
var pattern1 = /'/g; // with this pattern all single quotes are found;
var pattern2 = /'B|B'/g; // only single quotes before and after a space are found;
user_entry = user_entry.replace( pattern1, "somethingelse" );
main_fields[i].value = user_entry;
}
[/CODE]
[CODE]
for (var i=0; i<main_fields.length; i++){
// you can use one of the 2 lines below in your replace method:
var pattern1 = /'/g; // with this pattern all single quotes are found;
var pattern2 = /'B|B'/g; // only single quotes before and after a space are found;
main_fields[i].value = main_fields[i].value.replace( pattern1, "somethingelse" );
}
[/CODE]
And the combination < /'B|B'/g > finds & changes all single quotes that are before or after a space in mystring, so the single quote inside < angela's > is not touched.
[/quote]
[CODE]
function replacecomma(){
var fields=document.user_form.elements;
var target_pattern;
var main_fields=new Array();
for (var i=0; i<fields.length; i++){
if (fields[i].name=="text_field")
{main_fields.push(fields[i]);}
}
for (var i=0; i<main_fields.length; i++){
var user_entry=main_fields[i].value;
for (var j=0; j<user_entry.length; j++){
if (user_entry.charAt(j)=="'"){
var x=user_entry.charAt(j);
[COLOR="Blue"]var pattern=eval('/^((.)+(s)*)(('){0}){'+x+'}(('){0}))((s)*(.)+)$/');[/COLOR]
if(user_entry.match(pattern)!=null)
{
user_entry=user_entry.replace(RegExp(x,"g"), "something");
main_fields[i].value="";
main_fields[i].value=user_entry;
}
}
}
}
}
[/CODE]
Really appreciate everything...now for a couple of things...
I understand your approach. You are saying just get the value of the input field where if it displays a specific pattern I don't want (in this case single quotes), then that pattern is replaced and there's no need to loop through it to see if the single quote exists.
This is the issue...[COLOR="Blue"]Angela is supposed to be touched[/COLOR] . With your approach it doesn't appear that I'm looking out for the single quotes that may appear in other instances - like if it has an alpha, alphanumeric, symbol, space..etc in front or after it. You only look to see if a single quote is defined by the spacing between it and the next character. That's why[COLOR="Blue"]angela's[/COLOR] isn't touched. And if you do code for symbols, alpha, and alphanumeric chars in front/behind the single quote, then that means that you need to exclude the possibility of another single quote that may appear in front/behind, which means that it's a double quote.
[/QUOTE]
[CODE]
for (var i=0; i<main_fields.length; i++){
// you can use one of the 2 lines below in your replace method:
var pattern1 = /'/g; // with this pattern all single quotes are found;
var pattern2 = /'B|B'/g; // only single quotes before and after a space are found;
main_fields[i].value = main_fields[i].value.replace( pattern1, "somethingelse" );
}
[/CODE]
[CODE]
for (var i=0; i<main_fields.length; i++){
var pattern1 = /'/g; // with this pattern all single quotes are found;
main_fields[i].value = main_fields[i].value.replace( pattern1, "somethingelse" );
}
[/CODE]
Or did I miss another point?
Peter[/QUOTE]
[CODE]
var single = "'";
var double = '"';
single.charCodeAt(0); // 39
double.charCodeAt(0); // 34
String.fromCharCode(39); // "'";
String.fromCharCode(34); // """;
single > double; // true;
/'/ > /"/; // true; = the same comparison, written in regular expression literals;
var myarray = [ single, double ];
myarray.sort(); // [""", "'"]; double quote at the beginning, it has the lowest character code;
[ /'/g, /"/g ].sort(); // [ /"/g, /'/g ]; regular expression literals are sorted the same way;
[/CODE]
[CODE]
var twosingles = "''";
var twodoubles = '""';
String.fromCharCode(39, 39); // "''"; = the same string as var twosingles;
single + single; // "''"; evaluates to the same string as the line above;
twosingles === single + single; // true;
twodoubles === double + double; // true;
twosingles === double; // false;
[/CODE]
[CODE]
var empty1 = '';
var empty2 = "";
empty1 == empty2; // true;
[/CODE]
0.1.9 — BETA 6.17