Regex

Q

Québec

I am trying to make this onClick work:
onClick="FormatStr(('E',('F'(' G'('H')))))">

===============
var strTable = "\
<table border=1 width='100%'>\
<tr>\
<td width='50%'>@1</td>\
<td>@2</td>\
</tr>\
<tr>\
<td>@3</td>\
<td>@4</td>\
</tr>\
</table>"

function FormatStr(str) {
alert(arguments[0]+arguments[1]+arguments[2]+arguments[3]+arguments[4])
for ( i=1; i < arguments.length; i++ )
str = str.replace(new RegExp("@"+i), arguments);
return str;
}

function doIt() {
document.body.insertAdjacentHTML("beforeEnd", FormatStr(strTable, "A",
"B", "C", "D"));
}

<FORM >
<INPUT TYPE="button" value="Table" onClick="doIt()">
<INPUT TYPE="button" value="FormatStr" onClick="FormatStr(('E',('F'('
G'('H')))))">
</FORM>
 
T

Thomas 'PointedEars' Lahn

Québec said:
I am trying to make this onClick work:
onClick="FormatStr(('E',('F'(' G'('H')))))">

In Mozilla/5.0:
| Error: string is not a function
| Source File: javascript: alert(('E',('F'(' G'('H')))))
| Line: 1

In IE 6.0 SP-1 (translated):
| Line: 1
| Column: 1
| Error: Function expected
| Code: 0
| URL: [not of interest]

You are using the `(' operator without a keyword or an identifier
left-hand side or an expression inside. What did you want the above
argument(s) to display?
===============
var strTable = "\
<table border=1 width='100%'>\
<tr>\
<td width='50%'>@1</td>\
<td>@2</td>\
</tr>\
<tr>\
<td>@3</td>\
<td>@4</td>\
</tr>\
</table>"

Although supported in web browsers, you should not rely on `\' to continue
the string literal in the next line in all UAs as the JavaScript reference
specifies nothing of the kind. Use the concatenation operator `+' instead.


HTH

PointedEars
 
Q

Québec

doIt prints a table with the letters A B C D in each cell.

onClick="FormatStr('E', 'F',' G',('H'))))" is suppose to replace A B C D
with E F G H
 
T

Thomas 'PointedEars' Lahn

Québec said:
doIt prints a table with the letters A B C D in each cell.

onClick="FormatStr('E', 'F',' G',('H'))))" is suppose to replace
A B C D with E F G H

`(...)' outside of a string literal is nothing but a paranthesed
expression and thus here ('H') is the same as 'H'. And there are
more parantheses closed (4) than opened (2) here. I guess you are
looking for

onClick="FormatStr('E', 'F',' G', '(\'H\')')"

Besides,

function FormatStr(str) {
alert(arguments[0]+arguments[1]+arguments[2]+arguments[3]+arguments[4])
for ( i=1; i < arguments.length; i++ )
str = str.replace(new RegExp("@"+i), arguments);
return str;
}

will not work as supposed since "E" is assigned to `str' (as it is
the first argument or ...arguments[0]) and there is no `@x' in it to
replace.

function FormatStr() {
// i as local variable and arguments is zero-based
for (var i = 0; i < arguments.length; i++)
strTable =
strTable.replace(new RegExp("@" + (i + 1)), arguments);
return strTable;
}

will do the trick one time. If you need `strTable' unchanged, assign
its value to a local variable and refer to that variable for replace.

See
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/fcns.html#1008302
and
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/function.html#1193137
for details.


HTH

PointedEars
 
Q

Québec

I think, I have not been specific enough. The idea is to change the letters
in the already made table with the second function.
----------------
var strTable = "";
strTable += "<table border=1 width='100%'>"
strTable += "<tr>"
strTable += "<td width='50%'>@1</td>"
strTable += "<td>@2</td>"
strTable += "</tr>"
strTable += "<tr>"
strTable += "<td>@3</td>"
strTable += "<td>@4</td>"
strTable += "</tr>"
strTable += "</table>"

function FormatStr(str) {
for ( i=1; i < arguments.length; i++ )
str = str.replace(new RegExp("@"+i), arguments);
return str;
}

function doIt() {
document.body.insertAdjacentHTML("beforeEnd", FormatStr(strTable, "A",
"B", "C", "D"));
}


//--</script

Substitutes @1 with arg1, @2 with arg2 and so on at the passed
string.

<p><FORM >
<INPUT TYPE="button" value="doIt" onClick="doIt()">
<INPUT TYPE="button" value="FormatStr2" onClick="FormatStr2('E', 'F',' G',
'H')">
</FORM>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top