让我们说我们有 residence (住所)一词。可以通过js中的以下正则表达式进行匹配:
<template>
<main class="main detail-page">
<div>
<h1>Explore {{ title.rendered }}</h1>
<div class="detail-wrapper">
<section class="detail-intro detail-content-block"></section>
<section class="detail-map">
<p>Map</p>
</section>
</div>
<section class="detail-history">
<h1>History</h1>
<div class="detail-content-block"></div>
</section>
<section class="detail-wildlife">
<h1>Wildlife and Flora</h1>
<div class="detail-content-block"></div>
</section>
<section class="detail-places">
<h1>Places to See</h1>
</section>
<section class="detail-facilities">
<h1>Accommodation and Facilities</h1>
<div class="detail-content-block"></div>
</section>
<section class="detail-gettingthere">
<h1>Getting to </h1>
<div class="detail-content-block"></div>
</section>
</div>
</main>
</template>
<script>
export default {
created: function() {
},
data: function() {
return {
pageName: this.$nuxt.$route.params.slug,
pageRef: null,
pageContent: null,
pageType: 'single'
}
},
computed: {
exploreContent: function() {
return this.$store.state.explore
},
getSinglePage: function() {
return this.$store.state.pages
}
},
mounted: function() {
},
methods: {
}
}
</script>
<style lang="scss">
.detail-page {
h1 {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
display: block;
background-color: $crimson;
color: #fff;
text-align: center;
text-transform: uppercase;
margin: 0;
padding: 20px;
font-family: $font-title;
font-size: 28px;
font-weight: 400;
line-height: 1;
letter-spacing: 2px;
}
p {
font-family: $font-default;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
}
}
.detail-wrapper {
display: grid;
}
.detail-intro {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 2;
}
.detail-map {
grid-column-start: 4;
grid-column-end: 6;
grid-row-start: 1;
grid-row-end: 2;
min-width: 40vw;
min-height: 100%;
background-color: #cccccc;
text-align: center;
align-items: center;
justify-content: center;
p {
align-self: center;
justify-self: center;
}
}
.detail-content-block {
padding: 40px;
}
</style>
这很好,但是对我来说有一个问题: 所有字母都是可以互换的。此表达式还可以匹配:
residence.match( new RegExp(/[residence]{4,9}/, 'i' ) )
我想保留字符的顺序。正则表达式应匹配以下字符串:
ceresiden, denceresid, ence etc...
我该如何实现? 谢谢!
答案 0 :(得分:2)
使用lookahead测试有效的字符串,然后测试长度:
var test = [
'ceresiden',
'denceresid',
'ence',
'res',
'den',
'resid',
'sidence',
'ience',
'rednce',
];
console.log(test.map(function (a) {
return a + ' :' + a.match(/^(?=r?e?s?i?d?e?n?c?e?$).{4,9}$/);
}));
答案 1 :(得分:1)
因此,您希望它们以相同的顺序显示,但是可能要删除任意数量的字符?然后只需将每个字符设为可选:/(r?e?s?i?d?e?n?c?e?){4,9}/i
注意:由于{4,9}
量词的原因,此正则表达式以及您发布的正则表达式将与您指定的任何字符串都不匹配。匹配模式的字符串必须包括与“居住”(可选)模式连续至少4次(不带空格等)匹配的子字符串,以使其匹配。