(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[83211],{22122:function(e,t,n){"use strict";function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}n.d(t,{Z:function(){return r}})},17375:function(e,t,n){"use strict";function r(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},s=Object.keys(e);for(r=0;r<s.length;r++)n=s[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(r=0;r<s.length;r++)n=s[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}n.d(t,{Z:function(){return r}})},3905:function(e,t,n){"use strict";n.d(t,{Zo:function(){return u},kt:function(){return m}});var r=n(67294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},s=Object.keys(e);for(r=0;r<s.length;r++)n=s[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(r=0;r<s.length;r++)n=s[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var o=r.createContext({}),p=function(e){var t=r.useContext(o),n=t;return e&&(n="function"===typeof e?e(t):l(l({},t),e)),n},u=function(e){var t=p(e.components);return r.createElement(o.Provider,{value:t},e.children)},c={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},g=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,s=e.originalType,o=e.parentName,u=i(e,["components","mdxType","originalType","parentName"]),g=p(n),m=a,d=g["".concat(o,".").concat(m)]||g[m]||c[m]||s;return n?r.createElement(d,l(l({ref:t},u),{},{components:n})):r.createElement(d,l({ref:t},u))}));function m(e,t){var n=arguments,a=t&&t.mdxType;if("string"===typeof e||a){var s=n.length,l=new Array(s);l[0]=g;var i={};for(var o in t)hasOwnProperty.call(t,o)&&(i[o]=t[o]);i.originalType=e,i.mdxType="string"===typeof e?e:a,l[1]=i;for(var p=2;p<s;p++)l[p]=n[p];return r.createElement.apply(null,l)}return r.createElement.apply(null,n)}g.displayName="MDXCreateElement"},96086:function(e){"use strict";var t=Object.assign.bind(Object);e.exports=t,e.exports.default=e.exports},59601:function(e,t,n){"use strict";n.r(t),n.d(t,{default:function(){return u}});var r,a=n(22122),s=n(17375),l=n(67294),i=n(3905),o=(l.createElement,r="Intro",function(e){return console.warn("Component "+r+" was not imported, exported, or provided by MDXProvider as global scope"),(0,i.kt)("div",e)}),p={};function u(e){var t=e.components,n=(0,s.Z)(e,["components"]);return(0,i.kt)("wrapper",(0,a.Z)({},p,n,{components:t,mdxType:"MDXLayout"}),(0,i.kt)("h1",{id:"re"},"Re"),(0,i.kt)(o,{mdxType:"Intro"},(0,i.kt)("p",null,"Provide bindings to JS regular expressions (RegExp).")),(0,i.kt)("h2",{id:"t"},"t"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"prelude",prelude:!0}),"type t\n")),(0,i.kt)("p",null,"The RegExp object."),(0,i.kt)("h2",{id:"result"},"result"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"prelude",prelude:!0}),"type result\n")),(0,i.kt)("p",null,"The result of a executing a RegExp on a string."),(0,i.kt)("h2",{id:"captures"},"captures"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let captures: result => array<Js.nullable<string>>\n")),(0,i.kt)("p",null,"An ",(0,i.kt)("inlineCode",{parentName:"p"},"array")," of the match and captures, the first is the full match and the remaining are the substring captures."),(0,i.kt)("h2",{id:"matches"},"matches"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let matches: result => array<string>\n")),(0,i.kt)("p",null,"Deprecated. Use captures instead.\nAn ",(0,i.kt)("inlineCode",{parentName:"p"},"array")," of the matches, the first is the full match and the remaining are the substring matches."),(0,i.kt)("h2",{id:"index"},"index"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let index: result => int\n")),(0,i.kt)("p",null,"0-based index of the match in the input string."),(0,i.kt)("h2",{id:"input"},"input"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let input: result => string\n")),(0,i.kt)("p",null,"The original input string."),(0,i.kt)("h2",{id:"fromstring"},"fromString"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let fromString: string => t\n")),(0,i.kt)("p",null,"Constructs a RegExp object (Js.Re.t) from a ",(0,i.kt)("inlineCode",{parentName:"p"},"string"),".\nRegex literals ",(0,i.kt)("inlineCode",{parentName:"p"},'%re("/.../")')," should generally be preferred, but ",(0,i.kt)("inlineCode",{parentName:"p"},"fromString")," is useful when you need to dynamically construct a regex using strings, exactly like when you do so in JavaScript."),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let firstReScriptFileExtension = (filename, content) => {\n  let result = Js.Re.fromString(filename ++ "\\.(res|resi)")->Js.Re.exec_(content)\n  switch result {\n  | Some(r) => Js.Nullable.toOption(Js.Re.captures(r)[1])\n  | None => None\n  }\n}\n\n// outputs "res"\nfirstReScriptFileExtension("School", "School.res School.resi Main.js School.bs.js")\n')),(0,i.kt)("h2",{id:"fromstringwithflags"},"fromStringWithFlags"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let fromStringWithFlags: (string, ~flags: string) => t\n")),(0,i.kt)("p",null,"Constructs a RegExp object (",(0,i.kt)("inlineCode",{parentName:"p"},"Js.Re.t"),") from a string with the given flags.\nSee ",(0,i.kt)("inlineCode",{parentName:"p"},"Js.Re.fromString"),"."),(0,i.kt)("p",null,"Valid flags:"),(0,i.kt)("p",null,"g global\ni ignore case\nm multiline\nu unicode (es2015)\ny sticky (es2015)"),(0,i.kt)("h2",{id:"flags"},"flags"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let flags: t => string\n")),(0,i.kt)("p",null,"Returns the enabled flags as a string."),(0,i.kt)("h2",{id:"global"},"global"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let global: t => bool\n")),(0,i.kt)("p",null,"Returns a ",(0,i.kt)("inlineCode",{parentName:"p"},"bool")," indicating whether the global flag is set."),(0,i.kt)("h2",{id:"ignorecase"},"ignoreCase"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let ignoreCase: t => bool\n")),(0,i.kt)("p",null,"Returns a ",(0,i.kt)("inlineCode",{parentName:"p"},"bool")," indicating whether the ignoreCase flag is set."),(0,i.kt)("h2",{id:"lastindex"},"lastIndex"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let lastIndex: t => int\n")),(0,i.kt)("p",null,'Returns the index where the next match will start its search.\nThis property will be modified when the RegExp object is used, if the global ("g") flag is set.'),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let re = %re("/ab*/g")\nlet str = "abbcdefabh"\n\nlet break = ref(false)\nwhile !break.contents {\n  switch Js.Re.exec_(re, str) {\n  | Some(result) => Js.Nullable.iter(Js.Re.captures(result)[0], (. match_) => {\n      let next = Belt.Int.toString(Js.Re.lastIndex(re))\n      Js.log("Found " ++ (match_ ++ (". Next match starts at " ++ next)))\n    })\n  | None => break := true\n  }\n}\n')),(0,i.kt)("h2",{id:"setlastindex"},"setLastIndex"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let setLastIndex: (t, int) => unit\n")),(0,i.kt)("p",null,"Sets the index at which the next match will start its search from."),(0,i.kt)("h2",{id:"multiline"},"multiline"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let multiline: t => bool\n")),(0,i.kt)("p",null,"Returns a ",(0,i.kt)("inlineCode",{parentName:"p"},"bool")," indicating whether the multiline flag is set."),(0,i.kt)("h2",{id:"source"},"source"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let source: t => string\n")),(0,i.kt)("p",null,"Returns the pattern as a ",(0,i.kt)("inlineCode",{parentName:"p"},"string"),"."),(0,i.kt)("h2",{id:"sticky"},"sticky"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let sticky: t => bool\n")),(0,i.kt)("p",null,"Returns a ",(0,i.kt)("inlineCode",{parentName:"p"},"bool")," indicating whether the sticky flag is set."),(0,i.kt)("h2",{id:"unicode"},"unicode"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let unicode: t => bool\n")),(0,i.kt)("p",null,"Returns a ",(0,i.kt)("inlineCode",{parentName:"p"},"bool")," indicating whether the unicode flag is set."),(0,i.kt)("h2",{id:"exec_"},"exec","_"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let exec_: (t, string) => option<result>\n")),(0,i.kt)("p",null,"Executes a search on a given string using the given RegExp object.\nReturns ",(0,i.kt)("inlineCode",{parentName:"p"},"Some(Js.Re.result)")," if a match is found, ",(0,i.kt)("inlineCode",{parentName:"p"},"None")," otherwise."),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'/* Match "quick brown" followed by "jumps", ignoring characters in between\n * Remember "brown" and "jumps"\n * Ignore case\n */\n\nlet re = %re("/quick\\s(brown).+?(jumps)/ig")\nlet result = Js.Re.exec_(re, "The Quick Brown Fox Jumps Over The Lazy Dog")\n')),(0,i.kt)("h2",{id:"exec"},"exec"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let exec: (string, t) => option<result>\n")),(0,i.kt)("p",null,"Deprecated. please use ",(0,i.kt)("inlineCode",{parentName:"p"},"Js.Re.exec_")," instead."),(0,i.kt)("h2",{id:"test_"},"test","_"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let test_: (t, string) => bool\n")),(0,i.kt)("p",null,"Tests whether the given RegExp object will match a given ",(0,i.kt)("inlineCode",{parentName:"p"},"string"),".\nReturns true if a match is found, false otherwise."),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'/* A simple implementation of Js.String.startsWith */\n\nlet str = "hello world!"\n\nlet startsWith = (target, substring) => Js.Re.fromString("^" ++ substring)->Js.Re.test_(target)\n\nJs.log(str->startsWith("hello")) /* prints "true" */\n')),(0,i.kt)("h2",{id:"test"},"test"),(0,i.kt)("pre",null,(0,i.kt)("code",(0,a.Z)({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let test: (string, t) => bool\n")),(0,i.kt)("p",null,"Deprecated. please use ",(0,i.kt)("inlineCode",{parentName:"p"},"Js.Re.test_")," instead."))}u.isMDXComponent=!0,u.frontmatter={__ghEditHref:"https://github.com/reason-association/rescript-lang.org/blob/master/pages/docs/manual/v9.0.0/api/js/re.mdx"}},9783:function(e,t,n){(window.__NEXT_P=window.__NEXT_P||[]).push(["/docs/manual/v9.0.0/api/js/re",function(){return n(59601)}])}},function(e){e.O(0,[49774],(function(){return t=9783,e(e.s=t);var t}));var t=e.O();_N_E=t}]);