程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> it-怎麼為正則表達式匹配的字符串加上span包裹起來?

it-怎麼為正則表達式匹配的字符串加上span包裹起來?

編輯:編程解疑
怎麼為正則表達式匹配的字符串加上span包裹起來?

var re=/world|people|Pizar/g
var str = "Disney films are favored by people all around the world, it is known to all that many of the movies are produced by Pizar animation studio";

有一個正則表達式,還有一個字符串,我想為這個字符串中所有匹配正則表達式的單詞分別都加上一個< span >包裹起來,例如:< span >world< /span >一樣。請問怎麼才能辦到呢?

最佳回答:


 var re=/(world|people|Pizar)/g
var str = "Disney films are favored by people all around the world, it is known to all that many of the movies are produced by Pizar animation studio";
str=str.replace(re,'<span>$1</span>')
alert(str)
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved