4

I am trying to extract all IP addresses from an Apache log file input in the textarea field. I am using regular expressions for extracting the IP addresses. I want to see all the IP addresses printed on the screen. I cannot understand what I am doing wrong. Kindly help

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>RegEx_example</title>
</head>
<body style = "background-color: lightgrey">
<center>
<h3><u>Log Miner</u></h3>
<br /><hr />
<h5>Paste your Apache log file in the box below and click on Mine!</h5>

<textarea  rows="25" cols="200" form="mine_log" id = "logs">

</textarea>

<form id = "mine_log" method = "" onsubmit="parseLogs(document.getElementById('logs').value)">
    <input type = "submit" value = "Mine!!" />
</form>
<script language="JavaScript" type="text/javascript">
    function parseLogs(text) {
        var re = new RegExp("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$");
        var myArray = re.exec("text");
        document.write(myArray.toString());

    }
</script>
</center>
</body>
</html>
12
  • what issue are you having with the code now? Commented Aug 29, 2016 at 10:43
  • 1
    You surely need to try a regex literal var re = /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/g;. And use input.match(re). Commented Aug 29, 2016 at 10:44
  • @Reddy : it is not displaying anything. It should print all the occurrences of the addresses Commented Aug 29, 2016 at 10:45
  • 1
    You're testing string "test" instead of given variable Commented Aug 29, 2016 at 10:46
  • Remove ^anchors$. Commented Aug 29, 2016 at 10:46

2 Answers 2

2

You need to:

  • Use a regex literal to avoid double backslash escaping the shorthand classes (right now, "\." translates into .)
  • Remove anchors from the pattern (i.e. the ^ and $)
  • Add a global modifier to the regex (/g)
  • Use a String#match() with the regex (in case you do not need the values captured with the capturing groups, else, you need to run the RegExp#exec inside a loop to collect those).

function parseLogs(text) {
   var re = /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/g;
   var myArray = text.match(re);
   document.write("<pre>"+JSON.stringify(myArray, 0, 4) + "</pre>");
}
<h5>Paste your Apache log file in the box below and click on Mine!</h5>

<textarea  rows="5" cols="200" form="mine_log" id = "logs">
12.34.56.76
 45.45.34.24
</textarea>
<form id = "mine_log" method = "" onsubmit="parseLogs(document.getElementById('logs').value)">
    <input type = "submit" value = "Mine!!" />
</form>

Note that in case you need no captured values, you may even remove the ( and ) from your pattern.

Sign up to request clarification or add additional context in comments.

Comments

1

I see a typo in the code.

var myArray = re.exec("text"); you are just running the regex on the string text

should be

var myArray = re.exec(text); run the regex on the variable text

2 Comments

Typos are not real issues.
That doesn't help, but there are at least two more problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.