JavaScript regex on textarea input -
i trying extract ip addresses apache log file input in textarea field. using regular expressions extracting ip addresses. want see ip addresses printed on screen. cannot understand 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 apache log file in box below , 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>
you need to:
- use regex literal avoid double backslash escaping shorthand classes (right now,
"\."
translates.
) - remove anchors pattern (i.e.
^
,$
) - add global modifier regex (
/g
) - use
string#match()
regex (in case not need values captured capturing groups, else, need runregexp#exec
inside loop 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 apache log file in box below , 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 in case need no captured values, may remove (
, )
pattern.
Comments
Post a Comment