javascript正则表达式怎么多行匹配

JavaScript028

javascript正则表达式怎么多行匹配,第1张

i忽略大小写

g查找找到所有的匹配,而不是在找到第一个之后就停止

m多行模式,^匹配开头,$匹配结尾

varp=/"([^"]*[\n]*[^"]*)*"/gi

vararr=v.match(varp)

<html>

<head>

<script type="text/javascript">

//正则替换链接,链接有换行

function t1(){

var con = document.getElementsByName('content')[0].value//内容里有<a href="">.....</a>,但是有换行

var reg = /<a[\s]+[\d\D]*<\/a>/g//采用[\d\D]或[\w\W]或[\s\S]来解决不能换行问题

alert(con.replace(reg,'----'))

}

//正则把每一行的结尾数字,换成#号---多行模式,添加/m(每行当作结尾)和/g(全局匹配)模式增强符

function t2(){

var con = document.getElementsByName('content')[0].value//写几行文字,每行以数字结尾

var reg = /\d+$/gm

alert(con.replace(reg,'#'))

}

</script>

</head>

<body>

<textarea rows="5" cols="30" name="content"></textarea><br />

<button onclick="t1()">正则替换链接(需要考虑换行情况)</button><br />

<button onclick="t2()">正则多行替换</button><br />

</body>

</html>

const regex = /(.*)(id)(:|=)(\s*\S+)(.*)/gm

const str = `formatMessage({ id: 'app.settings.menu.focus' }),

<FormattedMessage id="app.settings.perferences.save" ></FormattedMessage>`

console.log(str.replace(regex,'$4'))

替换即可

var subject = '%BeginFont\r\nHandler:WinTTHandler\r\nFontType:Type1\r\nFontName:ACaslonPro-Bold\r\nFamilyName:Adobe Caslon Pro\r\nStyleName:Bold\r\nMenuName:Adobe Caslon Pro Bold\r\nStyleBits:0\r\nWeightClass:700\r\nWidthClass:5\r\nAngleClass:0\r\nFullName:Adobe Caslon Pro Bold\r\nWritingScript:Roman\r\nUsesStandardEncoding:yes\r\nisCFF:yes\r\nWinName:ACaslonPro-Bold\r\nFileLength:142728\r\nDesignSize:120\r\n%EndFont\r\n\r\n%BeginFont\r\nHandler:WinTTHandler\r\nFontType:Type1\r\nFontName:ACaslonPro-BoldItalic\r\nFamilyName:Adobe Caslon Pro\r\nStyleName:Bold Italic\r\nMenuName:Adobe Caslon Pro Bold\r\nStyleBits:1\r\nWeightClass:700\r\nWidthClass:5\r\nAngleClass:1\r\nFullName:Adobe Caslon Pro Bold Italic\r\nWritingScript:Roman\r\nUsesStandardEncoding:yes\r\nisCFF:yes\r\nWinName:ACaslonPro-BoldItalic\r\nFileLength:169548\r\nDesignSize:120\r\n%EndFont'

var result = []

subject.replace(/FullName:(.*)$/gm,function(m,c1){

result.push(c1)

})

console.log(result)