JavaScript Regex tester

This is a little JavaScript Regex tester, just write your Regex as example "\d" or "\w" and a test string like "123abc". It is more like an example as an real use function, but you can see how it works and handle the matches. Try it:

Matches:
Code:
<html>
<script>
function myFunction() {
if(document.getElementById("regex").value == ""){
document.getElementById("result").innerHTML = "No Regex";return(0);
}
var re = RegExp(document.getElementById("regex").value, 'g');
document.getElementById("result").innerHTML = 
re.execAll(document.getElementById("text").value);
}
RegExp.prototype.execAll = function(string) {
    var match = null;
    var matches = new Array();
    while (match = this.exec(string)) {
        var matchArray = [];
        for (i in match) {
            if (parseInt(i) == i) {
                matchArray.push(match[i]);
            }
        }
        matches.push(matchArray);
    }
    return matches;
}
</script>
<body>
<textarea id="regex" rows="1" cols="50" onchange="myFunction()" onkeyup="myFunction()" placeholder="Your Regex here..."></textarea>
<br>
<textarea id="text" rows="4" cols="50"onchange="myFunction()" onkeyup="myFunction()" placeholder="Your Text here..."></textarea>
<br>
Matches:
<div id="result"></div>
</body>
</html>

JavaScript generate and delete html inputs

This generates a new text box with a unique id if you hit the Add button, but if you hit the remove button it removes the last text box.
Try it:

   
<!DOCTYPE html>
<html>
<script>
var elements = 0;
function count(elem){
elements=0;
var xPathRes = document.evaluate ('//div[@id="'+elem+'"]/div', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var actualSpan = xPathRes.iterateNext ();
while (actualSpan) {
++elements;
actualSpan = xPathRes.iterateNext ();
}
}
function add(elem) {
        count(elem);
        if(elements <=10){
                document.getElementById(elem).innerHTML+='<div id="SPECIAL_DIV_'+elements+'" >TB'+elements+':<input type="text" id="SPECIAL_TB_'+elements+'" ></div>';
        }
}
function del(elem) {
        count(elem);
        if(elements >=1){
                document.getElementById("SPECIAL_DIV_"+(elements-1)).remove();
        }
}
</script>
<body>
<button onclick="add('demo');">Add</button>
<button onclick="del('demo');">Remove</button>
<br>
<div id="demo">
</div>
</body>
</html>
The count function counts the current text boxes, I used XPATH for this and then iterate trough the results to count them.

JavaScript in word press post

Sometimes I want to post some examples about JavaScript and html, but word press removes the JavaScript so it don't work, for that I installed the JavaScript-Inline-Plugin now you could wrote your code like that:



<html>
</html>
But don't forget so do this in the Text tab, and if you move back to Visual its gone... that's annoying but the best what I found ....

JavaScript replace all

This is a little example how to replace all matches with replace.
<!DOCTYPE html>
<html>
<body>

<p>Click the button to replace "i" with "X" in the paragraph below:</p>

<p id="demo">Visit Microsoft!</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var res = AllReplace(str,"i", "X");
    document.getElementById("demo").innerHTML = res;
}

function AllReplace(str,find,replace){
while(str != str.replace(find,replace)){
  str = str.replace(find,replace);
}
return(str);
}

</script>

</body>
</html>

Click the button to replace "i" with "X" in the paragraph below:

Visit Microsoft!