JS method to replace spaces in string_javascript skills
通常情况下输入域当中的 替换不掉(源代码当中有 ,页面上显示为空格),如果想替换掉,可以用另外手段。
增加一个隐藏域,值为 ,然后再替换
var sp=document.getElementById("space").value;
strData = document.all( "CommDN").value;
strData=strData.replace(sp,"");
js代码
function formatStr(str)
{
str=str.replace(/\r\n/ig,"
");
return str;
}
要注意两点:
要使用正则表达式,不能使用 str.replace("\r\n", newString); ,这会导致只替换第一个匹配的子字符串。
母字符串中不一定 \r\n 会同时存在,也许只有 \n,没有 \r 也是可能的。
replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则表达式对象(RegExp)也可以是字符串(string),replaceText是替代查找到的字符串。。为了帮助大家更好的理解,下面举个简单例子说明一下
Js代码
<script language="javascript"> var stringObj="终古人民共和国,终古人民"; //替换错别字“终古”为“中国” //并返回替换后的新字符 //原字符串stringObj的值没有改变 var newstr=stringObj.replace("终古","中国"); alert(newstr); </script>
比我聪明的你,看完上面的例子之后,会发现第二个错别字“终古”并没有被替换成“中国”,我们可以执行二次replace方法把第二个错别字“终古”也替换掉,程序经过改进之后如下:
Js代码
<script language="javascript"> var stringObj="终古人民共和国,终古人民"; //替换错别字“终古”为“中国” //并返回替换后的新字符 //原字符串stringObj的值没有改变 var newstr=stringObj.replace("终古","中国"); newstr=newstr.replace("终古","中国"); alert(newstr); </script>
我们可以仔细的想一下,如果有N的N次方个错别字,是不是也要执行N的N次方replace方法来替换掉错别字呢??呵,不用怕,有了正则表达式之后不用一个错别字要执行一次replace方法。。程序经过改进之后的代码如下
Js代码
<script language="javascript"> var reg=new RegExp("终古","g"); //创建正则RegExp对象 var stringObj="终古人民共和国,终古人民"; var newstr=stringObj.replace(reg,"中国"); alert(newstr); </script>
上面讲的是replace方法最简单的应用,不知道大家有没有看懂??下面开始讲稍微复杂一点的应用。。 大家在一些网站上搜索文章的时候,会发现这么一个现象,就是搜索的关键字会高亮改变颜色显示出来??这是怎么实现的呢??其实我们可以用正则表达式来实现,具体怎么样实现呢?简单的原理请看下面的代码
Js代码
<script language="javascript"> var str="中华人民共和国,中华人民共和国"; var newstr=str.replace(/(人)/g,"<font color=red>$1</font>"); document.write(newstr); </script>
上面的程序缺少互动性,我们再改进一下程序,实现可以自主输入要查找的字符
Js代码
<script language="javascript"> var s=prompt("请输入在查找的字符","人"); var reg=new RegExp("("+s+")","g"); var str="中华人民共和国,中华人民共和国"; var newstr=str.replace(reg,"<font color=red>$1</font>"); document.write(newstr); </script>
可能大家都会对$1这个特殊字符表示什么意思不是很理解,其实$1表示的就是左边表达式中括号内的字符,即第一个子匹配,同理可得$2表示第二个子匹配。。什么是子匹配呢??通俗点讲,就是左边每一个括号是第一个字匹配,第二个括号是第二个子匹配。。 当我们要把查找到的字符进行运算的时候,怎么样实现呢??在实现之前,我们先讲一下怎么样获取某一个函数的参数。。在函数Function的内部,有一个arguments集合,这个集合存储了当前函数的所有参数,通过arguments可以获取到函数的所有参数,为了大家理解,请看下面的代码
Js代码
<script language="javascript"> function test(){ alert("参数个数:"+arguments.length); alert("每一个参数的值:"+arguments[0]); alert("第二个参数的值"+arguments[1]); //可以用for循环读取所有的参数 } test("aa","bb","cc"); </script>
看懂上面的程序之后,我们再来看下面一个有趣的程序
Js代码
<script language="javascript"> var reg=new RegExp("\\d","g"); var str="abd1afa4sdf"; str.replace(reg,function(){alert(arguments.length);}); </script>
我们惊奇的发现,匿名函数竟然被执行了二次,并且在函数里还带有三个参数,为什么会执行二次呢??这个很容易想到,因为我们写的正则表达式是匹配单个数字的,而被检测的字符串刚好也有二个数字,故匿名函数被执行了二次。。在匿名函数内部的那三个参数到底是什么内容呢??为了弄清这个问题,我们看下面的代码。
Js代码
<script language="javascript"> function test(){ for(var i=0;i<arguments.length;i++){ alert("第"+(i+1)+"个参数的值:"+arguments); } } var reg=new RegExp("\\d","g"); var str="abd1afa4sdf"; str.replace(reg,test); </script>
经过观察我们发现,第一个参数表示匹配到的字符,第二个参数表示匹配时的字符最小索引位置(RegExp.index),第三个参数表示被匹配的字符串(RegExp.input)。其实这些参数的个数,还会随着子匹配的变多而变多的。弄清这些问题之后,我们可以用另外的一种写法
Js代码
<script language="javascript"> function test($1){ return "<font color='red'>"+$1+"</font>" } var s=prompt("请输入在查找的字符","人"); var reg=new RegExp("("+s+")","g"); var str="中华人民共和国,中华人民共和国"; var newstr=str.replace(reg,test); document.write(newstr); </script>
看了上面的程序,原来可以对匹配到的字符为所欲为。下面简单举一个应用的例子
Js代码
<script language="javascript"> var str="他今年22岁,她今年20岁,他的爸爸今年45岁,她的爸爸今年44岁,一共有4人" function test($1){ var gyear=(new Date()).getYear()-parseInt($1)+1; return $1+"("+gyear+"年出生)"; } var reg=new RegExp("(\\d+)岁","g"); var newstr=str.replace(reg,test); alert(str); alert(newstr); </script>
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.
