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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression 2

Status
Not open for further replies.

tekkerguy

Programmer
Nov 16, 2005
196
US
what regular expression would match:

Code:
onclick="tooltip.show

at the beginning of a string, and

Code:
');"

at the end.
 
First off, thanks for the quick reply. I hate regular expressions (I just started looking at them).

Second, is there a good tutorial site for them, I see tons on google., but which are good?
 
This will further restrict the dot position to the literal dot.
[tt] /^onclick="tooltip[red]\[/red].show.*'\);"$/[/tt]
 
This is the code I'm using:

Code:
 for ( i = 0; i < noOfRows; i++)
	    {
	    
var re = new RegExp(tableBody.rows[i].innerHTML, /^onclick="tooltip\.show.*'\);\"$/ )
	    
	    
	    tableBody.rows[i].innerHTML = tableBody.rows[i].innerHTML.replace(re, "");
	    
	    
	    

	    
	    }

And I'm getting this error: Microsoft JScript runtime error: Syntax error in regular expression


Is there something wrong in the code, or is it the regexp?
 
try this
Code:
for ( i = 0; i < noOfRows; i++)
        {
        
var re = new RegExp(/^onclick="tooltip\.show.*'\);\"$/ );
        
        
        tableBody.rows[i].innerHTML = tableBody.rows[i].innerHTML.replace(re, "");
        }

-----------------------------------------
I cannot be bought. Find leasing information at
 
slight change

Code:
onclick=\"tooltip.show(

for the beginning and

Code:
');\"

for the end,



(the dom modifies it a bit when it copies, just realized it was doing that)
 
Here's a simpler one, let's say the string is "Hello this world"

And I want to match hel at the beginning and orld at the end.


What's the regex for that? But it has to contain both.
 
so this should work then:



Code:
var re = /^on[.]*ick$/;
        stringtomatch = "onclick";
        
        matched = stringtomatch.search(re);


But it's returning -1, is something off in it?
 
Ok I fixed that, the [ ] were messing it up. So now that's returning a 0.

So I changed it a bit to this:

Code:
        var re = /^onclick.*ils$/;

        stringtomatch = "onclick=\" + "\"tooltip.show(r Details');\\";//\\" + "";

        
        
        matched = stringtomatch.search(re);

But that's returning a -1. Any ideas?

 
>for ( i = 0; i < noOfRows; i++)
>{
> var re = new RegExp(tableBody.rows[[ignore]i[/ignore]].innerHTML, /^onclick="tooltip\.show.*'\);\"$/)
> tableBody.rows[[ignore]i[/ignore]].innerHTML = tableBody.rows[[ignore]i[/ignore]].innerHTML.replace(re, "");
>}

Can you put it in words what you want to get done with that row's innerHTML? The definition of re is definitively wrong to the extend that the intention is not clear at all.
 
ok, this may clear things up a bit.

[1] If you want to use to use new RegExp() syntax, it is this.
[tt] var re = new RegExp("^onclick=\"tooltip\\.show.*'\\);\"$");[/tt]
where [tt]\"[/tt] is just to escape quote (") with respect to the string "...", but "\\." and "\\)" is proper escape with respect to the expression.

[1.1] It is of complete equivalent to
[tt]var re=/^onclick="tooltip\.show.*'\);"$/[/tt]

[2] If the replacement of the pattern by empty string is the purpose, you must revise the pattern to adapt to the purpose and those are important.

[2.1] The anchors "^" and "$" are no longer pertinent.
[2.2] The greediness of match has to be replaced by non-greedy version.
[2.3] The global flag has to be added if you anticipate more than one instance of matches. I add further the case insensitivity for html string.
[2.4] The version still has strong assumption on the quotes and non-significant whitespace degrees of freedom allowed in html recommendation. But it will go the ball rolling.
[tt]
var re = new RegExp([highlight]"o[/highlight]nclick=\"tooltip\\.show.*[highlight]?[/highlight]'\\);\[highlight]""[/highlight],"gi");
//or equivalently
//var re=/onclick="tooltip\.show.*'\);"/gi;
for (var i = 0; i < noOfRows; i++) {
tableBody.rows.innerHTML = tableBody.rows.innerHTML.replace(re, "");
}[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top