First, the basic
1. String .charAt (index) Gets a character string in accordance with a subscript
Application: the first letter of the string to determine whether capital
The number of any given sequence of letters, the statistics of the string inside uppercase and lowercase letters
2. String .indexOf ( "") subscript characters in the query string of the first occurrence of the (if not found, return -1) if the two parameters, and the second represents the first look from a few
Application: determining whether password special characters
string deduplication
3. String .lastIndexOf ( "") to find the location of characters in the string of the last occurrence of
Application: to determine a character in a character is not the only
Find no duplicate characters from a string
4. String .substring (start, end) string taken (taken from the start to the end does not include the start end) (the original string is not operating)
If indexStart equal indexEnd, substring returns an empty string.
If you omit indexEnd, substring extracts characters until the end of the string.
If any of the arguments is less than 0, or NaN, it is treated as zero.
If any parameter is greater than stringName.length, are treated as stringName.length.
If indexStart greater than indexEnd, the substring implementation of the results as two parameters, like swap.
The string .slice (start, end) string taken (taken from the start to the end start end not included) (accepts -1 for a negative argument starts from the last) (the original string is not operating) str.slice (-3, -1) antepenultimate not include a penultimate
6. String .substr (start, length) from the start of the start portion of length length taken. (If the argument is a case where one, several characters are deleted) (the original string is not operating)
7. .split string (delimiter) "Flip string (transfer array, the array rummaging then assigned to a string) (the original string is not operating)
8. String .replace (old, newStr) replaced without changing the original character string, and returns the new string can only replace a
Second, the case
-
Determine whether the correct user name length (oninput event, listening for the text box has changed. Element .oninput)
<INPUT type = "text" ID = "username">
<span ID = "info"> </ span>
username.oninput = function () {
IF (this.value.length> && this.value.length. 6 = < 10 =) { info.innerHTML = "valid user name length"; } the else { info.innerHTML = "user name length is not valid"; } }
-
Monitoring the number of message boards (oninput event, listening for the text box is changed or not. Elements .oninput)
css代码:
textarea {
width: 500px;
height: 200px;
resize: none;
}
em {
font-size: 22px;
color: red;
}
html代码:
<textarea name="" id="txt"></textarea>
<span id="info">剩余字数: <em id="text1">200</em></span>
js代码:
txt.oninput = function() {
text1.innerHTML = 200 - this.value.length ;
if (this.value.length >= 200) {
txt.disabled = 'true';
}
} -
Detecting the first letter of a string to uppercase or lowercase
STR = var "Hdhdhdhdhdhdhddaka";
IF (str.charAt (0)> = 'A' && str.charAt (0) <= 'the Z') {
the console.log ( "upper case");
} {the else
the console.log ( "not capitalized");
} -
Calculating a string, case number
var str = "lllllfkfLLLLLLLL";
var uppercase = 0;
var lowercase = 0;
for (var i = 0, k = str.length; i < k; i++) {
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
uppercase++;
} else {
lowercase++;
}
}
console.log("大写:" + uppercase + ";小写:" + uppercase); -
Determine whether the password contains special characters
var special = "#$%&*";
var str = "11111jdjdjjdd%^#$%jd&";
for (var i = 0, k = special.length; i < k; i++) {
if (str.indexOf(special.charAt(i)) != -1) {
console.log("有特殊字符");
break;
}
} -
Judge a character in a character is not the only
= STR "asdfghds"; function onlyChar (STR, C) { IF (str.indexOf (C) == -1) { the console.log (C + "unique") } {the else the console.log (C + "are not the only "); } } onlyChar (STR, 'a')
-
From there look for the string there is no repeating characters
str = "asdfghds";
function strNoRepeat(str) {
strTemp = "";
for (var i = 0, k = str.length; i < k; i++) {
if (str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))) {
strTemp += str.charAt(i);
}
}
return strTemp;
}
console.log(strNoRepeat(str)); -
Sensitive word filtering
-
analysis
Button binding events to
get input values input
filter content sensitive words, instead of using *
value input in after filtration, into a div. -
Code
Method a: var = Special [ 'Fuck', 'MD', 'Kao', 'laji']; btn.onclick = function () { // Get the value of the text box var inputVal = txt.value; // filtered sensitive words for (var I = 0; I <special.length; I ++) { for (var J = 0; J <inputVal.length; J ++) { inputVal = inputVal.replace (Special [I], '*') ; } } // write the value after filtering the div content.innerHTML = inputVal; } method using two :( global regular expression matching) var Special = [ 'Fuck', 'MD', 'Kao', 'laji']; btn.onclick = function () { // Get the value of the text box var inputVal = txt.value; for (var I = 0; I <special.length;++ I) { var new new REG = the RegExp (Special [I], "g"); // regular expression represents the global matching g
// Loop through sensitive words inputVal = inputVal.replace (REG, '*'); } content.innerHTML = inputVal; }
-
-
Codes strength
-
analysis
Length is 6-16
all-digital suggesting a weak
function of special characters (! @ # Etc.) strongly suggesting
otherwise prompt -
Code
css code:
<INPUT type = "text" ID = "PSW"> <span ID = "info"> </ span>
HTML Code: span { font-size: 12px; margin-left: 10px; } JS Code: var = Special "@ # $% ^ & *"; // find the elements on the page, the binding event psw.oninput = function () { var pswVal = psw.value; IF (pswVal.length <6 || pswVal.length > 16) { return info.innerHTML = "illegal password length!"; } ! IF (typeof (Number The (pswVal)) == 'Number' && isNaN (pswVal)) { return info.innerHTML = "password strength weak! "; } // IF (Number The (pswVal) == pswVal) { // return info.innerHTML =" password strength weak "! // } for (var i = 0; i < special.length; i++) {
IF (pswVal.indexOf (Special [I]) = -1!) { return info.innerHTML = "Strong Password strength!"; } } return info.innerHTML = "Password intensity"; }
-