|
I have a good, working regex. I have a function which takes a string, and process it a little and return a desirable new string.
I have a huge string which has multiple locations where my regex would match it.
What I want to do is: Take my regex, somehow find all occurrences of substring that matches my regex, process and replace those substring with the function i described above.
Trouble: One would try to do string.replace(regix, newString); as a natural thing, but it doesn't quite work because the newString depends on whatever my regix has captured.
Then the next thing one would try would be string.replace(regix, some regex containing $1) Here $1 is whatever my regix has captured, assume I captured it with ( ). However, the new string cannot be expressed nicely as a regex, and I'd have to use the function for further process.
What I really really wanted is to somehow say this: string.replace(regix, f($1)) That would be ideal, however $1 is not really a string object and I have failed to cast it as one...
What do I do? T__T
|
If I understand you correctly, I think you need to add a g at the end of your regexp (like /[0-9]/g) to make it global, as without it the regexp will only handle the first occurance.
Edit: nm now I see what you're saying. I think your best bet is to run the string several times with different regexps if you need different replacements for each kind.
... Or you could run the regexp in a search without the global tag, take the return data and process it, which would be the first occurance, and then you can run the regexp (not global) replace once with the result. Loop.
Think that would work?
edit2: seems search just returns the position where the match occured. Simple version, use two strings, one to retrieve results from and one to insert them into.
|
On September 08 2010 22:23 Osmoses wrote: If I understand you correctly, I think you need to add a g at the end of your regexp (like /[0-9]/g) to make it global, as without it the regexp will only handle the first occurance.
my regex has /g in it.
|
Warning: I don't know javascript So you want to replace the actual occurences in the bigger string? Some pseudo-code would help but I'm guessing this is what you want to do:
note: pseudo for the sake of having an example
string a = "hello world!" string b = (regex, a) to get "hello" if (b == "hello") string.replace(a.b, "hi");
string a should now = "hi world!"
I think I would try to store the location of the regex match, for example if the match starts at bigString[50] and is 10 characters long, create a new bigString2, make bigString2[0..49] = bigString[0..49], then sub in the new string from the regex'd string. Again, I have no idea what kind of shennanigans you can do to your javascript strings so I someone is probably going to stroll on in and do it in one line.
|
What is $1 and where do you get it from?
|
$i references subpatterns, which are denoted by parentheses.
His problem is that while regex returns these subpatterns and even use them in replacement, it's sort of a black box so you can't arbitrarily modify these subpatterns and replace them.
Most straightforward way I can think of, save the matches, then replace the patterns with a non-present pattern, split the string by the new pattern. Then concatenate the pieces with the modified pattern matches.
$original_string; $final_string = ""; $match = regular_expression_match_all($original_string, $pattern) $pattern_replaced_string = regular_expression_replace( $original_string, $pattern, "ARTOSISJOINSC2TERRAN" ) $pieces_array = split( $pattern_replaced_string, "ARTOSISJOINSC2TERRAN" )
foreach ( $pieces_array as $piece using index $i ) { $final_string .= $piece . f( $match[i] ); }
of course there's one more piece than match, but whatever.
|
|
Your question is incredibly confusing... Try to at least provide a sample of the string you are trying to parse and the expected result.
http://www.gskinner.com/RegExr/ is a good tool for building/testing regex. If you can explain your problem more clearly I can try to help further.
|
Maybe the /g flag causes the capture groups to not output strings, does it replace one time with no /g flag at the end? If it can do that just put that in an empty while loop or something.
|
|
That's awesome, will definitely remember that for the future
|
|
|
|