By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,964 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,964 IT Pros & Developers. It's quick & easy.

Regex

P: n/a
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[i]);
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>
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Québec wrote:
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
Jul 20 '05 #2

P: n/a
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

Jul 20 '05 #3

P: n/a
Québec wrote:
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[i]);
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[i]);
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/...s.html#1008302
and
http://devedge.netscape.com/library/...n.html#1193137
for details.
HTH

PointedEars
Jul 20 '05 #4

P: n/a
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[i]);
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>
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.