[英]Replace text in paragraph with JavaScript
只是
all_string.replace('null', 'No Info');
這樣一來,你可以像disannulling轉的話會變成disanNo Infoing。 因此,您應該使用regex來匹配規范空字:
all_string.replace(/\bnull\b/g, 'No Info');
例
var element = document.querySelector('p'); element.innerHTML = element.innerHTML.replace(/\\bnull\\b/g, 'No Info');
<p>this is the first null line. another null null null. thisnull will not be replaced. </p>
要替換文本,可以使用String.prototype.replace()函數。
您可以像這樣使用它:
YOUR_STRING = YOUR_STRING.replace(/\bnull\b/g, "No info");
檢查一下: http : //jsfiddle.net/a9osg2zp/
您可以使用替換功能。
如果要替換倍數,則需要使用正則表達式。
例如,
"hello hello".replace("hello","goodbye")
輸出: "goodbye hello"
"hello hello".replace(/hello/g,"goodbye")
輸出: "goodbye goodbye"
while(APIString.indexOf('null') != -1){ APIString.replace('null','No info'); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.