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

Regex question 2

Status
Not open for further replies.

Mpdreamz

Programmer
Jan 17, 2005
14
DE
function showmn(txt) {
var txt = txt.replace(\x02(.*?)(?:\x02|$),'<b>\1</b>')
this.document.body.innerHTML=this.document.body.innerHTML+txt;
}

its a valid regex that does what i want in several languages and regex tools but in javascript i get an error saying an object is expected and sign 25 is wrong :/ any idea's ? i use this to replace mircs bold codes to proper html codes
 
I don't think you can use regular expressions like that in a string.replace call. You would have to use a real regular expression. Unfortunately, javascripts regular expression object only does testing and parsing, not replacing.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
?! You certainly can use regular expressions in a replace.

var txt = txt.replace(/\x02(.*?)(?:\x02|$)/gi,"<b>$1</b>")

delimit the pattern with forward slashes, include a "g" for a global search, an "i" for case insensitivity.

Or you could create a regular expression object and pass it into the first parameter of the replace method.

You can also use them in a split() if you want.

Adam
 
Adam, you're correct, of course. I've never had occasion to need to use a re in a replace, and I'm not sure it was possible back when I first learned js. I guess I also assumed that since they added a regular expression object, there was no other ability to use regular expressions.

Even those of us who have been around a while can still learn something new, even if it does come at the expense of making oneself look foolish.

Can you use regular expression "tagged expressions" or "referbacks" in the replacement string? MSDN doesn't say so, it just says a string.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
OMFG i cant believe i completely forgot the /gi :O :O tnxs for the $1 pointer :D :D about referbacks as replacement strings in many scripting languages you can it works in The regex coach and according to the javascript regular expresion tutorial ive been reading you can as well.
 
for some odd reason in the Regex Coach
(.*?)(?:|$)/xx
with replacement <u>\1</u>
it makes this line <b>HEHEHE</b>  ok so whats the dealio ?  hmmm
turn into <u><b>HEHEHE</b> </u> ok so whats the dealio ? <u> hmmm</u>

which is exactly what i wanted lol
BUT yes javascript doesnt like me using 
and the regex isnt responding on \x31 like it was for \x02 for the bold replacement where JS didnt like 
what is the deal with this char ? and cant i return it someother way as in mircscripting for instance you can use $chr(31) something likewise possible in mIRCscripts ?

this is what i have now.
function showmn(txt) {
var txt = txt.replace(/\x02(.*?)(?:\x02|$)/gi,"<b>$1</b>")
var txt = txt.replace(/\x31(.*?)(?:\x31|$)/gx,"<u>$1</u>")
this.document.body.innerHTML=this.document.body.innerHTML+txt;
}
 
Try:
Code:
String.fromCharCode(31)


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 

function showmn(txt) {
var txt = txt.replace(/\x02(.*?)(?:\x02|$)/gi,"<b>$1</b>")
var txt = txt.replace(/txt.fromCharCode(31)(.*?)(?:txt.fromCharCode(31)|$)/gx,"<u>$1</u>")
this.document.body.innerHTML=this.document.body.innerHTML+txt;
}
Ok i think i need some serious help lol tnxs so much for helping me this far, i know that regex wont work but how can i set a var to (evaluate charcode(31)) + (.*?)(?: (evaluate charcode (31)) + |$)/gx ??
 
There are two ways to create a regular expression. One is to use the re delimiter (/) instead of quotes, the other is to create a regular expression object. The RegExp constructor takes a normal, quoted string for the actual expression part, which means you can use concatenation and expressions/functions to create your regular expression.
Code:
var myRe = new RegExp(String.fromCharCode(31) + "(.*?)(?:" + String.fromCharCode(31) + "|$)", "gi");
txt = txt.replace(myRe,"<u>$1</u>");


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top