JS Regex to match a string with a set of 3 non-concsecutive characters

南笙酒味 提交于 2021-01-07 04:50:14

问题


So i have a string of numbers that i want to run a series of matching tests on.

i have a string of numbers that represent locations on a board. it looks something similar to '024578'

Then I test those strings for matches that represent three in a row. So testing for three in the same row is easy, because the numbers are consecutive. i just used the following code:

const rowsRegex = /([012]{3}|[345]{3}|[678]{3})/g;

now to check for three in the same column is proving difficult for my limited regex knowledge. i have three sets of non-consecutive numbers and don't know how to write the regex to match a string if all three numbers of a set are present in the string.

i tried variations on the idea i used for rows for the last hour or so on regex101, but havent had any success. i figure it would look something like the following:

const colsRegex = /([036]|[147]|[258])/g;

so with the string 013567, how can i match with the first set in colsRegex ?

thank you kindly for any and all help.


回答1:


Try this RegExp:

/^\d*0\d*3\d*6\d*$/

It should work, given it's copied and pasted from a comment you accepted as working.



来源:https://stackoverflow.com/questions/61538913/js-regex-to-match-a-string-with-a-set-of-3-non-concsecutive-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!