在本教學中,我們學習如何使用JavaScript RegExp找到括號中的數字。數字(0-9)的ASCII值從48到57。我們在正規表示式中用[0-9]表示括號中的數字。要找到除所有數字之外的範圍內的數字,我們可以寫出特定的範圍。例如,要找出4和8之間的數字,我們可以在正規表示式模式中寫為[4-8]。現在,我們的目標是使用JavaScript中的RegExp在文字中找到括號內的數字。我們可以按照以下語法來找出括號中的數字。
Syntax
以下是RegExp組[0-9]字元的語法 -
new RegExp("[0-9]") or simply /[0-9]/
/[0-9]/, is introduced in ES1. It is fully supported by all browsers. Like, Chrome, IE, Safari, Opera, FireFox and Edge.
RegExp有修飾符,如g,i,m。 "g"用於執行全域匹配,"i"用於執行不區分大小寫的匹配,"m"用於執行多行匹配。
Syntax for /[0-9]/ with modifier like
new RegExp("[0-9]", "g") or simply /[0-9]/g
Algorithm
- 步驟 1 − 定義一個包含一些數字的字串。
- STEP 2 − Define the RegExp pattern for digits between brackets.
- 步驟 3 - 在上述定義的字串上套用match(pattern)函數,以便在字串中尋找括號之間的數字。
- 第四步 - 顯示結果- 匹配的數字。
讓我們看一些程式範例,以便更清楚地理解。
Example 1
In the program below, we use string match(pattern) to find digits between 1 and 4 in the given string. We use RegExp pattern as /[1-4]/g. The string match() method returns an array of digits in the string.
<!DOCTYPE html> <html> <body> <h2 id="Finding-digits-inside-the-bracket">Finding digits inside the bracket</h2> <p id = "text"></p> <p>Digits inside the bracket [1-4] : <span id= "result"></span> </p> <script> let myStr = "0127845639Hello"; document.getElementById("text").innerHTML = myStr; let pattern = /[1-4]/g; let result = myStr.match(pattern); document.getElementById("result").innerHTML = result; </script> </body> </html>
Here, text is given as 0-9 digits and Hello word. In the pattern, we have given [1-4] only. match() method will search digits from 1 to 4 only. If mentioned digits found in the text, match() method will return an array of existing digits otherwise it will return as null. Let's see another example.
Example 2
In the program below, we take a string with no digits and try to find digits in the string. We use string match(pattern) to find digits between 1 and 4 in the given string. We use the RegExp pattern as / [1-4]/g. See what our output looks like.
<!DOCTYPE html> <html> <body> <h1 id="Finding-digits-inside-the-bracket">Finding digits inside the bracket</h1> <p id= "result"></p> <script> let text = "567890"; let pattern = /[1-4]/g; let result = text.match(pattern); if(result == null){ document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets"; } else { ocument.getElementById("result").innerHTML = result; } </script> </body> </html>
Here, we can observe in the pattern we have mentioned [1-4] but in the text we are given from 5-9 and 0. match() method will return as null because there are no findings. So, if the statement is executed. If input text is given as the first example, then match() will return an array of existing digits and another statement will be executed. Like,
Example 3
<!DOCTYPE html> <html> <body> <h1 id="Finding-digits-inside-the-bracket">Finding digits inside the bracket</h1> <p id= "result"></p> <script> let text = "0127845639Hello"; let pattern = /[1-4]/g; let result = text.match(pattern); if(result == null){ document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets"; } else { document.getElementById("result").innerHTML = "Digit(s) inside the inside the bracket: " + result; } </script> </body> </html>
Now, We will check how to replace word character(s) in a given text. Let’s see an example
Example 4
在括號之間找出並取代數字
在下面的範例中,我們使用split()和join()方法找到並取代1和4之間的數字為空格字元。
<!DOCTYPE html> <html> <body> <h1 id="Replace-digits-inside-the-bracket">Replace digits inside the bracket</h1> <p>After replacing the digits inside the bracket : <span id= "result"></span> </p> <script> let text = "0127845639Hello"; let pattern = /[1-4]/g; let result = text.split(pattern).join(" "); document.getElementById("result").innerHTML = result; </script> </body> </html>
Example 5
We will also check to replace the digits inside the bracket using a simpler way. Like,
<!DOCTYPE html> <html> <body> <h1 id="Replace-digits-inside-the-bracket">Replace digits inside the bracket</h1> <p>After replacing the digits inside the bracket : <span id= "result"></span> </p> <script> let text = "0127845639Hello"; let pattern = /[1-4]/g; let result = text.replace(pattern , " "); document.getElementById("result").innerHTML = result; </script> </body> </html>
As we discussed, g for global matches. Instead of stopping with the first occurrence, it will look for all the occurrences.
Hope this tutorial will give knowledge on how to find digits inside the brackets using RegExp in JavaScript.
以上是在JavaScript的RegExp中找出括號中的數字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

HTML的未來趨勢是語義化和Web組件,CSS的未來趨勢是CSS-in-JS和CSSHoudini,JavaScript的未來趨勢是WebAssembly和Serverless。 1.HTML的語義化提高可訪問性和SEO效果,Web組件提升開發效率但需注意瀏覽器兼容性。 2.CSS-in-JS增強樣式管理靈活性但可能增大文件體積,CSSHoudini允許直接操作CSS渲染。 3.WebAssembly優化瀏覽器應用性能但學習曲線陡,Serverless簡化開發但需優化冷啟動問題。

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

HTML的未來充滿了無限可能。 1)新功能和標準將包括更多的語義化標籤和WebComponents的普及。 2)網頁設計趨勢將繼續向響應式和無障礙設計發展。 3)性能優化將通過響應式圖片加載和延遲加載技術提升用戶體驗。

HTML、CSS和JavaScript在網頁開發中的角色分別是:HTML負責內容結構,CSS負責樣式,JavaScript負責動態行為。 1.HTML通過標籤定義網頁結構和內容,確保語義化。 2.CSS通過選擇器和屬性控製網頁樣式,使其美觀易讀。 3.JavaScript通過腳本控製網頁行為,實現動態和交互功能。

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增強WebevebDevelopment。

HTML是構建網頁結構的基石。 1.HTML定義內容結構和語義,使用、、等標籤。 2.提供語義化標記,如、、等,提升SEO效果。 3.通過標籤實現用戶交互,需注意表單驗證。 4.使用、等高級元素結合JavaScript實現動態效果。 5.常見錯誤包括標籤未閉合和屬性值未加引號,需使用驗證工具。 6.優化策略包括減少HTTP請求、壓縮HTML、使用語義化標籤等。

HTML是一種用於構建網頁的語言,通過標籤和屬性定義網頁結構和內容。 1)HTML通過標籤組織文檔結構,如、。 2)瀏覽器解析HTML構建DOM並渲染網頁。 3)HTML5的新特性如、、增強了多媒體功能。 4)常見錯誤包括標籤未閉合和屬性值未加引號。 5)優化建議包括使用語義化標籤和減少文件大小。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境