JS科学计数法分解
来源:互联网 发布:贝丽花园淘宝店 编辑:程序博客网 时间:2024/04/29 17:55
在进行字符串分析提取有用子串时,我们使用正则表达式使我们的内容变的简单!
建议使用RegExp对象,当然,你完全可以使用直接量的方式构建你的正则表达式,此对象的构建方式:
var regExp = new RegExp('正则表达式', '标记');
如果我们使用正则表达式完成字符串的分组提取,就得注意这个标记了,RegExp对象有个exec(..)方法,而这个exec方法与我们构建表达式对象的[g]标记关系又比较密切,regExp.exec()方法无论在是否含有g标记时都会返回一个匹配数组,该数组第一个元素array[0]存储完全匹配的字符串, array[1] 当我们使用分组表达式的时候也是全局字符串array[0]==array[1],而array[2]将会是我们分组提取的表达式! 这是一个匹配科学计数法,并提取相应字符的示例:
view plaincopy to clipboardprint?
01./**
02. * 科学计数法的转换
03. * @author wyc
04. */
05.function convertNUM(beforeCountTest) {
06. //转换之前的科学计数法表示
07. var tempValue = beforeCountTest.value;
08. var tempValueStr = new String(tempValue);
09. if ((tempValueStr.indexOf( 'E' ) != -1) || (tempValueStr.indexOf( 'e' ) != -1)){
10. alert(tempValueStr + '是科学计数法表示!' );
11. var regExp = new RegExp( '^((//d+.?//d+)[Ee]{1}(//d+))$', 'ig' );
12. var result = regExp.exec(tempValue);
13. var resultValue = "" ;
14. var power = "" ;
15. if (result != null ){
16. resultValue = result[2];
17. power = result[3];
18. result = regExp.exec(tempValueStr);
19. }
20. if (resultValue != "" ) {
21. if (power != "" ) {
22. var powVer = Math.pow(10, power);
23. alert( "10的" + power + "次方[" + powVer + "]" );
24. resultValue = resultValue * powVer;
25. }
26. }
27. $( 'afterCountTest' ).value=resultValue;
28. }
29.}
/**
* 科学计数法的转换
* @author wyc
*/
function convertNUM(beforeCountTest) {
//转换之前的科学计数法表示
var tempValue = beforeCountTest.value;
var tempValueStr = new String(tempValue);
if ((tempValueStr.indexOf( 'E' ) != -1) || (tempValueStr.indexOf( 'e' ) != -1)){
alert(tempValueStr + '是科学计数法表示!' );
var regExp = new RegExp( '^((//d+.?//d+)[Ee]{1}(//d+))$', 'ig' );
var result = regExp.exec(tempValue);
var resultValue = "" ;
var power = "" ;
if (result != null ){
resultValue = result[2];
power = result[3];
result = regExp.exec(tempValueStr);
}
if (resultValue != "" ) {
if (power != "" ) {
var powVer = Math.pow(10, power);
alert( "10的" + power + "次方[" + powVer + "]" );
resultValue = resultValue * powVer;
}
}
$( 'afterCountTest' ).value=resultValue;
}
}
--------------------------------------------------------------------------------
·注意:在构建RegExp时必须有g标记,当有g标记的时,exec方法执行一次以后,会自动将RegExp的一个特殊属性lastIndex置为此次匹配的字符串的最后一个字母的下一个位置,当再次执行的时候,会在lastIndex位置开始查找,而不是 0 位置开始查找。如果exec没有找到匹配的字符串,它将自动将lastIndex置为 0。这个特殊的方法,可以很方便的循环遍历整个字符串,以找到所有匹配的子字符串..
- JS科学计数法分解
- JS科学计数法分解
- js科学计数法处理
- JS科学计数法初探
- js 科学计数法转换为小数点
- js 数字转为科学计数法
- js数字串传参时变科学计数法
- js把科学计数法形式的数据转换成非科学计数法的形式
- 科学计数法 E
- 科学计数法
- JAVA 科学计数法
- PAT1024 科学计数法
- JAVA科学计数法
- PAT1024科学计数法
- pat_b_1024 科学计数法
- 1024 科学计数法
- C++科学计数法
- 科学计数法
- HDMI EDID, PC EDID, DDC, IIC总线四者之间的关系
- CreateRemoteThread
- Log4Net使用指南
- Eclipse 安装配置指南
- 如何判断国际会议是否被EI收录
- JS科学计数法分解
- 慎用 strip (1) arm linux
- ACL 是如何工作的
- 良好的监控架构
- 20110510 Brew工程师培训笔记——第十章 IDisplay使用简介
- 将BMP图像读成数组
- Ubuntu 10.04操作小总结
- 隐藏DataGridView中无数据的列
- android 3D 游戏实现之人物行走(MD2)