Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A matter on replace function

Status
Not open for further replies.

keremito1985

Programmer
Sep 12, 2007
2
TR
The below function replaces first "z" characters with "_" of every word starts with z. (zeal and zebra changes _eal and _ebra)
The question is how can i replace first "p" chars with "*" in addition to the above func?
mean, if someone fills a textbox like ;

zeal pist zebra pear will come to "_eal *ist _ebra *ear"

=======the code=============
function toUpper() {
var pattern = /(\w)(\w*)/;
var a = document.form1.box.value.split(/\s+/g);
for (i = 0 ; i < a.length ; i ++ ) {
var parts = a.match(pattern);
var firstLetter = parts[1].replace(/z/i, "_");
var restOfWord = parts[2].toLowerCase();
a = firstLetter + restOfWord;
}
document.form1.box.value = a.join(' ');
}
 
[1] You name the function toUpper() but change char to lower case apart from the replacing z seems self-misleading! That's not me to say how to name it.

[2] The function can _much_ simplified and make concise. I add the requested replacement of p on top of the z and you'll the difference.
[tt]
function toUpper2() {
var a = document.form1.box.value.split(/\s+/g);
for (var i=0;i<a.length;i++) {
a=a.replace(/^([zZ])(\w*)$/,"_$2");
a=a.replace(/^([pP])(\w*)$/,"*$2");
a=a.toLowerCase();
}
document.form1.box.value = a.join(' ');
}
[/tt]
 
Thanks a million TSUJI, anyway it works really fine, but i tried some other non-asc?? chars like;

replace ? with I and i with ? but did not work, any idea?

thanks again.
 
You tried... what have you tried?!!! Even the question is not clear. ?,i,I any position or first postion. One version (any position and final outcome can contain "i" in lower case) is this.
[tt]
function toUpper2() {
var a = document.form1.box.value.split(/\s+/g);
for (var i=0;i<a.length;i++) {
a=a.replace(/^([zZ])(\w*)$/,"_$2");
a=a.replace(/^([pP])(\w*)$/,"*$2");
a=a.replace(/\?/g,"I");
a=a.replace(/i/g,"?");
a=a.toLowerCase();
}
document.form1.box.value = a.join(' ');
}
[/tt]
Anything not shown and variations are your own effort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top