JavaScript 对象(三) 之Boolean对象、Math对象和RegExp对象

4.JavaScript Boolean对象

(1)创建Boolean对象的方法:

var boolTemp = new Boolean(value);//构造函数

var boolTemp = Boolean(value);//转换函数

<html>
<body>
<script type="text/javascript">
	//创建初始值为false的Boolean对象
	var boolTemp = new Boolean();
	var boolTemp = new Boolean(0);
	var boolTemp = new Boolean(null);
	var boolTemp = new Boolean("");
	var boolTemp = new Boolean(false);
	var boolTemp = new Boolean(NaN);

	//创建初始值为true的Boolean对象
	var boolTemp = new Boolean(1);
	var boolTemp = new Boolean(true);
	var boolTemp = new Boolean("true");
	var boolTemp = new Boolean("false");
	var boolTemp = new Boolean("Tersia");
</script>
</body>
</html>

 

5.JavaScript Math对象

注意:Math对象并不像Date和String等,是对象的类,因此它没有构造函数,像Math.round()只是函数,而不是某个对象的方法。因此无需创建Math对象,只需将Math作为对象使用就可以调用其所有的属性和方法。

如:var pi_value = Math.PI;

      var sqrt_value = Math.sqrt(15);

(1)Math对象的常用方法

1)round()方法把一个数字舍入最接近的整数,即四舍五入;

    ceil()方法对一个数进行向上取整;

     floor()方法对一个数进行向下取整。

<html>
<body>
<script type="text/javascript">
	document.write(Math.round(0.60) + "<br />")
	document.write(Math.round(0.50) + "<br />")
	document.write(Math.round(0.49) + "<br />")
	document.write(Math.round(-4.40)+ "<br />")
	document.write(Math.round(-4.60)+ "<br />")

	document.write(Math.ceil(0.60) + "<br />")
	document.write(Math.ceil(0.50) + "<br />")
	document.write(Math.ceil(0.49) + "<br />")
	document.write(Math.ceil(-4.40)+ "<br />")
	document.write(Math.ceil(-4.60)+ "<br />")

	document.write(Math.floor(0.60) + "<br />")
	document.write(Math.floor(0.50) + "<br />")
	document.write(Math.floor(0.49) + "<br />")
	document.write(Math.floor(-4.40) + "<br />")
	document.write(Math.floor(-4.60))
</script>
</body>
</html>

2)random()方法可返回介于0-1之间的一个随机数。

<html>
<body>
<script type="text/javascript">
	//返回0.0-1.0之间的一个随机数
	document.write(Math.random()+"<br/>");
	
	//返回0-10之间的一个随机数
	document.write(Math.round(Math.random()*10)+"<br/>");
	document.write(Math.floor(Math.random()*11));
</script>
</body>
</html>

3)max(x,y...)方法可返回多个数中最大的那个数值

    mix(x,y,...)方法可返回多个数中最小的那个数值

<html>
<body>
<script type="text/javascript">
	document.write(Math.max(5,7,10,99,8) + "<br />")
	document.write(Math.max(-3,5) + "<br />")
	document.write(Math.max(-3,-5) + "<br />")
	document.write(Math.max(7.25,7.30)+ "<br />")

	document.write(Math.min(5,7,10,99,8,0) + "<br />")
	document.write(Math.min(-3,5) + "<br />")
	document.write(Math.min(-3,-5) + "<br />")
	document.write(Math.min(7.25,7.30))
</script>
</body>
</html>


6.JavaScript RegExp对象

RegExp对象表示正则表达式,它是对字符串执行模式匹配的。

(1)创建RegExp对象:var patt=new RegExp(pattern,attributes);

                                      var patt = /pattern/attributes;

pattern:是字符串,指定了正则表达式的模式或其他正则表达式。attributes:是可选的字符串,包含“g”执行全局匹配;"i"执行对大小写不敏感的匹配;"m"执行多行匹配。注意:如果pattern是正则表达式,而不是字符串,必须省略attributes。

<html>
<body>
<script type="text/javascript">
	var txt = "Hello Whole World";
	var pattern = /O/g;
	var pattern1 = /o/g;
	var pattern2 = /world/i;
	document.write(txt.match(pattern)+"<br/>")
	document.write(txt.match(pattern1)+"<br/>")
	document.write(txt.match(pattern2));
</script>
</body>
</html>

(2)RegExp的方法

1)test()方法:用于检测一个字符串是否匹配某个模式,有匹配返回True,否则false。

RegExpObject.text(string str);

<html>
<body>
<script type="text/javascript">
	var txt = "Hello Whole World!";
	var patt = new RegExp("world","i");
	var patt1 = /whole/i;
	document.write(patt.test(txt)+"<br/>");
	document.write(patt1.test(txt));
</script>
</body>
</html>

2)exec()方法:用于检索字符串中的正则表达式的匹配,返回一个数组,其中存放匹配的结果。如果没有找到匹配,返回null.

RegExpObject.exec(string str);

注意:当RegExpObject不是一个全局正则表达式时,exec()与String.match()返回的数组是相同的。

          当RegExpObject是一个全局正则表达式时,exec()会在 RegExpObject 的 lastIndex 属性指定的字符处开始检索字符串 string。当 exec() 找到了与表达式相匹配的文本时,在匹配后,它将把 RegExpObject 的 lastIndex 属性设置为匹配文本的最后一个字符的下一个位置。可以通过反复调用exec()来遍历字符串中的所有匹配的文本。当exec()再也找不到匹配的文本时,返回null,并把lastIndex属性重置为0。可以说,在循环中反复地调用exec()方法是唯一一种获得全局模式的完整模式匹配信息的方法。

<html>
<body>
<script type="text/javascript">
	var txt = "Hello world World!";
	var patt = new RegExp("world","gi");
	var result;
	while((result=patt.exec(txt))!=null)
	{
		document.write(result+"<br/>");
		document.write(result.lastIndex+"<br/>");
	}
</script>
</body>
</html>

3)compile()方法:用于编译或改变重新编译正则表达式。
RegExpObject.compile(regexp,modifier);

regexp:正则表达式;modifier:“g”,"i","gi"。

<html>
<body>
<script type="text/javascript">
	var txt = "Every man in the world! Every woman on the earth!"
	patt=/man/g;
	txt1=txt.replace(patt,"person");
	document.write(txt1+"<br/>");

	patt=/(wo)?man/g;
	patt.compile(patt);
	txt2 = txt.replace(patt,"person");
	document.write(txt2);
</script>
</body>
</html>

(3)支持正则表达式的String方法

1)search()方法:用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
stringObject.search(regexp),注意:要忽略大小写,加"i"。返回stringObject中第一个与regexp相匹配的子串的起始位置。如果没有找到则返回-1.

<html>
<body>
<script type="text/javascript">
	var txt = "Every man in the world! Every woman on the earth!"
	patt=/man/g;
	document.write(txt.search(patt)+"<br/>");
	document.write(txt.search(/every/)+"<br/>");
	document.write(txt.search(/every/i));
</script>
</body>
</html>

2)match()方法:在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

stringObject.match(string searchvalue);

stringObject.match(regexp);

返回存放匹配结果的数组。

<html>
<body>
<script type="text/javascript">
	var txt = "Every man in the world! Every woman on the earth!"
	document.write(txt.match("world")+"<br/>");
	var str = "1 plus 2 equal 3";
	document.write(str.match(/\d/g));
</script>
</body>
</html>

3)stringObject.repalce(regexp/substr,replacement)方法:用于在字符串中用一些字符替换另一些字符,或者替换一个与正则表达式匹配的子串。

<html>
<body>
<script type="text/javascript">
	var txt = "Every man in the world! Every woman on the earth!"
	document.write(txt.replace(/man/,"woman")+"<br/>");
	document.write(txt.replace(/Every/g,"")+"<br/>");
	document.write(txt.replace("the","whole"));
</script>
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值