JavaScript实例1

JavaScript不只是简单的构建块

JavaScript对象、基本类型和字面值之间的区别

对象:Object windwow 数组、正则、日期、原型、使用 new 通过构造方法创建的
基本类型:字符串、数值、布尔类型、null和undefined,只有字符串、数组和布尔值有构造函数,其余两种都是字面值
字面值

var str1 = "this is a  simple string";
var num1 = 1.45;
var answer = true;

当你使用严格相等性来比较一个对象示例和一个字面值的时候,可以快速区分出一个基本类型和一个对象实例:即基本类型变量严格地等于字面值

var str1 = String("string");
var num1 = Number(1.45);
var bool1 = Boolean(true);

if(str1 ==="string") 
  console.log('equal');
if(num1 === 1.45)
  console.log('equal');
if(bool1 === true)
  console.log('equal');

var str2 = new String("string");
var num2 = new Number(1.45);
var bool2 = new Boolean(true);

if(str2 ==="string") 
  console.log('equal');
else  console.log('not equal');
if(num2 === 1.45)
  console.log('equal');
else  console.log('not equal');
if(bool2 === true)
  console.log('equal');
else  console.log('not equal');
输出:
"equal"
"equal"
"equal"
"not equal"
"not equal"
"not equal"

从字符串提取出一个列表

1 使用 trim去掉多余的空格
2 使用正则表达式去掉多余的空格

var sentence = 'This is one sentence.This is a sentence with a list of items:'+
    'cherries , oranges , apples,bananas.That was the list of lists.';
var start = sentence.indexOf(':');
var end = sentence.indexOf('.',start+1);
var listStr = sentence.substring(start+1,end);

var fruits = listStr.split(',');
fruits.forEach(function(elem,index,array){
  array[index] = elem.trim();
});
var fruits2 = listStr.split(/\s*,\s*/);
console.log(fruits);
console.log(fruits2);

检查一个存在的、非空的字符串

//var unknownVairable = new String("string");
var unknownVairable = "string";
if(((typeof unknownVairable != 'undefined' && unknownVairable) && unknownVairable.length > 0) && typeof unknownVairable.valueOf()=='string'){
  console.log(true);
}

插入特殊的字符

解决方案:使用转义序列,例如要想添加到页面的一段文本中添加一个版权符号
转义序列最重要的用法之一,是在双引号或单引号分隔开的字符串中,包含双引号或单引号:

var newString = 'you can\'t use single quotes '+
    'in a string surrounded by single quotes.'+
    'Oh,wait a sec... yes you can.';
console.log(newString);
输出:
"you can't use single quotes in a string surrounded by single quotes.Oh,wait a sec... yes you can."

找到并突出显示一个模式的所有实例

var searchString = "Now is the time and this is the time and that is the time";
var pattern = /t\w*e/g;
var matchArray;
var str = "";
while((matchArray = pattern.exec(searchString))!=null){
  str+= "at  "+ matchArray.index+" we found "+ matchArray[0] +"\n";

}
console.log(str);
输出:
"ime";
var pattern = /t\w*e/g;
var matchArray;
var str = "";
while((matchArray = pattern.exec(searchString))!=null){
  str+= "at  "+ matchArray.index+" we found "+ matchArray[0] +"\n";

}
console.log(str);
var re = /a(p+).*(pie)/ig;//全局标志,触发了RegExp对象去保留每一次匹配的位置,并且从之前找到的匹配之后开始搜索。
var result = re.exec("The apples in the apple pie are tart");
console.log(result);
console.log(result.index);
console.log(result.input);
Console Run  Clear
"at  7 we found the
at  11 we found time
at  28 we found the
at  32 we found time
at  49 we found the
at  53 we found time
"

var re = /a(p+).*(pie)/ig;
var result = re.exec("The apples in the apple pie are tart");
console.log(result);
console.log(result.index);
console.log(result.input);
输出:
["apples in the apple pie", "pp", "pie"]
4
"The apples in the apple pie are tart"

使用exec和全局标志来查找并突出显示一个文字字符串中的所有匹配

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Searching for strings</title>
  <style type="text/css">
    .found{
        background-color: #ff0;
    }
  </style>
</head>
<body>
  <form id="textsearch">
    <textarea name="" id="incoming" cols="150" rows="10"></textarea>
    <p>Search pattern:<input type="text" id="pattern" name=""></p>
  </form>
  <button id="searchSubmit">Search for pattern</button>
<div id="searchResult"></div>

  <script type="text/javascript">
  window.onload= function(){
    document.getElementById("searchSubmit").onclick = function(){
        //获取模式
        var pattern = document.getElementById("pattern").value;
        var re = new RegExp(pattern, "g");

        //获取字符串
        var searchString = document.getElementById("incoming").value;

        var matchArray;
        var resultString ="<pre>";
        var first = 0;
        var last = 0;

        while((matchArray = re.exec(searchString))!=null){
            last = matchArray.index;
            //获取所有匹配的字符串,将其连接起来
            resultString += searchString.substring(first, last);

            //使用class,添加匹配的字符串
            resultString +="<span class='found'>"+matchArray[0]+"</span>";
            first = re.lastIndex;
        }

        //完成字符串
        resultString += searchString.substring(first,searchString.length);
        resultString +="</pre>";

        //插入到页面
        document.getElementById("searchResult").innerHTML = resultString;
    }
  };

  </script>
</body>
</html>


这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值