views:

79

answers:

2

I have a long string and I need to find instances of '#!#'+some text+'#!#'

right now I have:

string.replace(/(#!#*#!#)/g, function (m) {....});

I need the whole thing passed into a function like that so that I can replace them correctly.

However, I want m to only be equal to what lies between the two #!#..I want this part..#!#

but what I return needs to replace the entire #!#'+some text+'#!#

If it matters the text between the two #!#'s will be either an integer or a sentence, but it won't contain the pattern #!# of course.

+4  A: 
/(#!#*#!#)/

Close, but the * makes it mean “a number of instances of ‘#’, which is probably not what you meant. Try:

/#!#(.*?)#!#/

(The *? means match as little as possible up to the #!#. Otherwise if there are two #!# sequences, the expression will match ‘greedily’ up to the last one.)

bobince
Note that the arguments to the replace function will be the whole matched string (including the #!#'s) then the part between the #!#'s.
Matthew Crumley
A: 

Try to parse again when you get the "big" word, like so

"llala #!#11#!# asdad".replace(/#!#(.*?)#!#/gi, function(m) { return m.match(/#!#(.*?)#!#/)[1];})
Sergej Andrejev