JavaScript RegExp toString() 方法
javascriptfront end technologyobject oriented programming
RegExp toString() 方法用于返回正则表达式的字符串值。
以下是 RegExp toString() 方法的代码 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } div { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript RegExp toString() method</h1> <div class="sample">The quick brown fox jumps over the lazy dog</div> <button class="btn">点击此处</button> <div class="result"></div> <h3>Click on the above button to get the string value of regular expression.</h3> <script> let fillEle = document.querySelector(".sample"); let result = document.querySelector(".result"); let regex = new RegExp("fox",'g'); document.querySelector(".Btn").addEventListener("click", () => { result.innerHTML = regex.toString(); }); </script> </body> </html>
输出
点击“CLICK HERE”按钮 −