Problem with RegExpr
geradok
Posts: 9
I spent an hour trying to get this working:
string = "My STRING=15";regx = new RegExp( "(\d+)", "g" )//regx = /([A-Z]+)=(\d+)/;//regx = /(\d+)/g;//regx = /[\w]+/g//stringObj = String(string)//print( stringObj.indexOf( regx ))print( string.indexOf( regx ))
The result is always -1. None of the outcommented variants would help.
By chance (honestly by searching :-) I found a snippet for replacing a backslash by a slash that worked. So I changed my code into:
string = "My STRING=15";regx = new RegExp( "(\\d+)", "g" )regx = /([A-Z]+)=(\d+)/;regx = /(\d+)/g;regx = /[\w]+/gprint( string.indexOf( regx ))print( string.lastIndexOf( regx ))print( string.replace( regx, "xXx" ))
Now the replacement always works, while the indices returned are always -1.
Does that mean that indexOf and lastIndexOf don't work with RegExpr ? Or did I miss something?
Comments
Instead of indexOf() I now use search() which works. For lastIndexOf() I seem to need a workaround.
\ is the escape character, if you want a literal \ in a string you must double it - the first says the following character is a special, the enxt says what the special is. \t is a tab, \n is a new line and so on.