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 help

Status
Not open for further replies.

jsteph

Technical User
Oct 24, 2002
2,562
US
Hi,
I'm trying to create a regexp to use in a .replace method with the followig logic:

I want to take the below string:
"some gibberish, could be anything blah blah something worthwhile"
...and return:
"new gibberish something REALLY worthwhile"

Where in the initial string 'some gibberish, could be anything blah blah' could be any text or any characters, but NOT CONTAIN the specific phrase "foo"--and up to but not including the specific phrase 'something worthwhile'.

I've tried:
Code:
var s1 = "some gibberish, could be anything blah blah something worthwhile"
var s2="some foo gibberish, could be anything blah blah something worthwhile"

var sfind = /(.*[^foo])something worthwhile/
s1=s1.replace(sfind,myfunc) //This is successful for s1

s2=s2.replace(sfind,myfunc) //this should NOT find a match since 'foo' is part of s2 prior to 'something worthwhile', but it matches and returns the same as s1

function myfunc(match,gibberish){
gibberish = "new gibberish"
return gibberish + ' something REALLY worthwhile'
}

So I'm basically having trouble constructing it so it will catch anything PRIOR TO 'something worthwhile' but not if that prior string includes the phrase 'foo'.
Thanks for any help,
--Jim


 
jsteph,

Exclusion certain pattern within a positive identification pattern is not very suitable (even impossible) for single regex pattern. Use a two-step approach would greatly simplify the task and becoming more realistic.

So start with pattern like you're using.
[tt] var sfind = /.*something worthwhile/;[/tt]
Then filter the acceptible result using indexOf, like this.
[tt] s.match(sfind).toString().indexOf("foo")==-1[/tt]
as conditional.

That is the general approach.

- tsuji
 
You're pretty unclear as to what happens to any other text following the matching text, but you could try something like:
Code:
function f(s, foo, bar, r) {
  var i1, i2;

  i1 = s.indexOf(bar);
  if (i1 >= 0) {
    i2 = s.indexOf(foo);
    if (i2 < 0 || i2 > i1) {
      return r + s.substr(i1 + bar.length);
    }
  }
  return s;
}
alert(f("some gibberish, could be anything blah blah something worthwhile", "foo", "something worthwhile", "something REALLY worthwhile"));
alert(f("some foo gibberish, could be anything blah blah something worthwhile", "foo", "something worthwhile", "something REALLY worthwhile"));
alert(f("some gibberish, could be anything blah blah something worthwhile but not here", "foo", "something worthwhile", "something REALLY worthwhile"));
alert(f("some gibberish, could be anything blah blah something worthwhile but foo not here", "foo", "something worthwhile", "something REALLY worthwhile"));
 
Thanks very much. tsuji, I think that that will probably be the way I go.
MOrac, that appears to be a procedural way of accomplishing the same task so it should work too.

I'm not sure from a performance standpoint if regular expressions or procedural string parsing would prove faster. I've got to do this on hundreds of rows of data. My current regular expression is taking quite a while (it's more involved than my example above), but prior to that I was doing looping over a big string variable, which was even slower.

I'll give these a try, thanks again,
--Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top