开发者

Removing a particular line from a paragraph using javascript

I pass a string from one page to another page using AJAX method.This string is available in the data base.It retrieves string along with the whole client page script of the page from where i am retrieving the data.I want the string alone from the rest of the data using javascript.The string which i retrieve from the data base alone will be keep on changing This is how it looks:

         **live its live** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"> <head><title>  Untitled Page </title>

I want to re开发者_StackOverflow中文版move the whole paragraph except **live its live**.The problem is that the **live its live** text alone will be changed if it is updated in the data base

Can anybody pls help me sort out this problem....


demo

var str = '**live its live**   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>  Untitled Page </title><link href="App_Themes/Modern/default.css" type="text/css" rel="stylesheet" /></head> <body>     <form name="form1" method="post" action="/Web/MessageDisplay.aspx" id="form1"> <div> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTa4056JeZHQioLQvNmbYjBQvHt8A==" /> </div>      <div>          </div>     </form> </body> </html>';    

alert(str.slice(0,str.indexOf('<!DOCTYPE')))​


Your code must have a way to uniquely identify the live its live text to avoid loopholes. If you just slice the string using the content it may fail. Assuming that you used "**" as a wrapper to uniquely identify the text, an alternative to slicing the string or getting its substring is to use javascript's regular expression.

E.g.

var re = /\*\*.*\*\*/gi;

var str =  '**live its live** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"> <head><title>  Untitled Page </title>;';
alert(re.exec(str)[0]);

For more information on RegExp, see https://developer.mozilla.org/en/core_javascript_1.5_guide/regular_expressions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜