Пропустить навигацию

Лекция №4

<body>
<p>The test() method returns true if it finds a match, otherwise it returns false.</p>
<p>Click the button to check an e-mail</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var str = "erterterte";
    var patt = new RegExp("^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([a-z\.]{2,6})$");
    var res = patt.test(str);
    document.getElementById("demo").innerHTML = res;
}
</script>
</body>

 

 

 

var reg =/\d/;
var input = “hhkj7rg334i2uhnkejn23d”

 

reg.exec(input);
[ ‘7’,
index: 4,
input: ‘hhkj7rg334i2uhnkejn23d’ ]

 

 string01.match(/\d{3,6}/)
[ ‘425324’, index: 5, input: ‘rtyfn4253243432dbdd3d3dd3’ ]



 

 

<body>
<p>Search a string and display the match:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var str = "rtyfn4253243432dbdd3d3dd3";
    var n = str.match(/\d{3,6}/i);
   document.getElementById("demo").innerHTML = n + ", <br>" + n.index + ", <br>" + n.input;   
}
</script>
</body>


 

 

<body>
<p>Search a string and display the position of the match:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var str = "rtyfn4253243432dbdd3d3dd3";
    var n = str.search(/\d{3,6}/i);
    document.getElementById("demo").innerHTML = n;
}
</script>
</body>

 

<body>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft!</p>
<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML;
    var txt = str.replace(/microsoft/i,"MATI");
    document.getElementById("demo").innerHTML = txt;
}
</script>
</body>



 

 

 

var str = “Windows is the best OS because windows is good and also windows is simple!”;
 str.replace(“windows”, “Linux”);
// Windows is the best OS because Linux is good and also windows is simple!”;
str.replace(/windows/ig, “Linux”);
// Linux is the best OS because Linux is good and also Linux is simple!”;

 

 

 

function func() {
  alert('Привет');
}
setTimeout(func, 1000);

 

 

<input type="button" onclick="clearInterval(timer)" value="Стоп">

<script>
  var i = 1;
  var timer = setInterval(function() { alert(i++) }, 2000);
</script>