Home >Web Front-end >JS Tutorial >Expansion and repair of strings in javascript framework design for reading notes_javascript skills
1.repeat method: Repeat a string n times. For example: repeat("chaojidan",2) -> chaojidanchaojidan
Method 1:
Method 2:
3. Convert camel case style: str.replace(/[-_][^-_]/g,function(match){return match.charAt(1).toUpperCase();})
//-_In [], there is no need to use it, and ^ in [] means the opposite, that is, when -a or _a is encountered, it will be replaced by A (match is a regular matching string _a, then take a and capitalize it)
4. Convert to underline style: str.replace(/([a-zd])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase ();
//The first replace matches the string of cA or 4A, and then replaces it with c_A or 4_A. $1 represents the first subexpression. The second replacement is to use _ to replace -. Since - is not in [], it needs to be added.
5. Remove the html tag in the string: str.replace(/<[^>] >/g,''), which will remove the script tag, but will not remove the js script in the script.
6. Remove the script tag and remove the js script inside: str.replace(//img,'')
/ Need to be used to prevent escaping.
//(Ss)*?) Match as little as possible, non-greedy matching. For example: <script>aaa</script>dddd<script>bbbb</script> will match <script>aaa</script> first, then <script>bbbb</script>, if not Adding ? will be a greedy match, and will match all <script>aaa</script>dddd<script>bbbb</script>, even the string dddd will be removed.
7. Escape the string through html to obtain content suitable for display on the page.
str.replace(/&/g,'&').replace(//g,'>').replace(/"/ g,'"').replace(/'/g,''');
8. Replace the html entity characters of the string with corresponding characters:
The opposite of 7, just one more replace(/([d] );/g,function($0,$1){ return String.fromCharCode(parseInt($1,10)) }) //$1 is The first subexpression match.
9.trim:str.replace(/^s | s $/g,'') , IE or early standard browsers do not list many blank characters as s, so there will be bugs. However, why insist on being compatible with obsolete browsers?