通常情况下输入域当中的 替换不掉(源代码当中有 ,页面上显示为空格),如果想替换掉,可以用另外手段。
增加一个隐藏域,值为 ,然后再替换
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>
以上所述就是本文的全部内容了,希望大家能够喜欢。

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.