views:

82

answers:

4

Hey guys,

wonder if you could help me out with some Javascript. I have a string that is log message, and i'm waniting to grab a section out of this log and display that on it's own

eg.(bear in mind the log section is not case sesitive and any letter can be any case, also could be placed anywhere withing the string)

$LOG: 08880xbpnd $ fhdsafidsfsd df sd fsd f sd fsd

thats the orignal log, Im wanting to grab the 08880xbpnd and get rid of the rest? how can this be done in javascript?

thanks for the help matt

edit

if this is any help i have this regex in perl that grabs the log somewhere else

/(?i)\$LOG:\s*(none|(temp(GD|MD)(.\d{1,2}){4}))\s*\$/

basically what ever in between $LOg: and $ i want to grab and exclude any white space that value in between could be anything or that above

A: 

Matthew,

the following regex should give you anything between $LOG: and $ in backreference \1

\$LOG:(.*?)\$
Lieven
sorry but how would you call that in javascript?
Matthew De'Loughry
I don't know javascript but I guess you could do the same with this expression as is outlined in the excepted answer.
Lieven
duh... "accepted" anwser
Lieven
+2  A: 

This works for me :

<script type="text/javascript">
  // Call the function with a sample input string.
  GetMyLog("$LOG: 08880xbpnd $ fhdsafidsfsd df sd fsd f sd fsd");

  function GetMyLog(fullString)
  {
    // Create a Regex object. We want to capture all word-like characters within the $LOG and ending $
    // This assumes that there will not be any more "$" characters in the trailing string.
    var reg = /\$LOG:\s*([\w]+)\s*\$/;

    // If the match attempt was successful, we need to get the second value in the array returned by the match.
    if (fullString.match(reg))
    {
      alert(reg.exec(fullString)[1]);
    }
  }
</script>
Cerebrus
Yours is the only regex that excludes whitespace, as per the OP's requirements, so +1 for you ;)
LukeH
Although you missed out the fact that it needs to be case-insensitive.
LukeH
Thanks for the vote, @Luke! My stress in this particular answer was to write the code in JS, rather than perfect the Regex. Great observation, though! :-)
Cerebrus
+3  A: 

I would use this regular expression:

/\$LOG:([^$]+)\$/i

So:

"$LOG: 08880xbpnd $ fhdsafidsfsd df sd fsd f sd fsd".match(/\$LOG:([^$]+)\$/i)
Gumbo
Agreed! That is a better Regex than the one I presented. +1 :-)
Cerebrus
Yours is the only regex that's case-insensitive, as per the OP's requirements, so +1 for you ;)
LukeH
Although you missed out the fact that it needs to exclude whitespace.
LukeH
You can trim those afterwards.
Gumbo
A: 

I won't use regex, your case is simple and, maybe, using substring you can obtain a gain in speed.

Try using something like this:

function getToken(logline, startTag, endTag) {
  return logline.substring(startTag.length,logline.indexOf(endTag,1));
}

new version in response of a comment:

function getToken(logline, startTag, endTag) {
  result =""; 
  if ( logline.indexOf(startTag)>=0) 
    result= logline.substring(startTag.length,logline.indexOf(endTag,1));
  return result;  
}
Eineki
this will not work if the $log: tag is not present within the log
Matthew De'Loughry
Oh, in your question there are any hints (at least i missed them) that the loglines can have different formats
Eineki