javascript - Regex number filtering from textarea -
i have been looking way how filter data within textarea using regex function quite while without success. below regex want use filter uk telephone numbers.
(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?
fiddle: https://jsfiddle.net/qdypo04y/
i want achieve result when button clicked remove lines not meet regex? alternatively remove values not uk telephone numbers.
any guidance appreciated.
apart use of textarea element issue is:
- how attach click event listener button (refer to: queryselector , addeventlistener)
- how content of textarea , split rows (refer to: textcontent plus split , join)
- finally how use regex: refer test
an example is:
document.queryselector('button').addeventlistener('click', function(e) { var txtarea = document.queryselector('textarea[rows="4"][cols="50"]'); var re = /(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?/; var txtarr = txtarea.textcontent.split('\n'); txtarr.foreach(function(ele, idx) { txtarr[idx] = ele + ' test result is: ' + re.test(ele); }); txtarea.textcontent = txtarr.join('\n'); });
<textarea rows="4" cols="50"> +447222555555 0800 042 0213 2017/07/14 2017/07/17 2017/07/27 </textarea> <button>click me</button>
Comments
Post a Comment