admin管理员组文章数量:1402949
String can contain anything, but must have one asterisk (*
) throughout the string and that asterisk can be anywhere in the string.
Also string should not contain any blank spaces.
Following are valid strings:
test*
*_test
test*something
Following are invalid strings:
test_**
**_test
test*something*
test *something
test *
testsomething
*
Someone please help me writing a regex for the above scenario.
String can contain anything, but must have one asterisk (*
) throughout the string and that asterisk can be anywhere in the string.
Also string should not contain any blank spaces.
Following are valid strings:
test*
*_test
test*something
Following are invalid strings:
test_**
**_test
test*something*
test *something
test *
testsomething
*
Someone please help me writing a regex for the above scenario.
Share Improve this question edited Apr 18, 2016 at 13:13 Deve asked Apr 18, 2016 at 13:02 DeveDeve 1151 silver badge8 bronze badges 9- Is a one-character string that is just an asterisk valid? – nnnnnn Commented Apr 18, 2016 at 13:04
- So it's a string that starts with zero or more non-asterisk characters, followed by a single asterisk, followed by zero or more non-asterisk characters? – Pointy Commented Apr 18, 2016 at 13:06
- Asterisk can be anywhere in the string.But, should have only one asterisk.If there is no asterisk in the string then it's invalid.Also, If there are more than one asterisk in the string then it's invalid.If there is one asterisk then it's a valid string it must return 'true' – Deve Commented Apr 18, 2016 at 13:09
- @nnnnnn no,it's not a valid string – Deve Commented Apr 18, 2016 at 13:11
-
^[^*\s]*\*[^*\s]*$
– Huso Commented Apr 18, 2016 at 13:14
4 Answers
Reset to default 4Use this RegEx:
^(?!(.*?\*){2,}|.*? |\*$).*?\*.*$
Live Demo on Regex101
If you want it to allow tabs (all other whitespace), use \s
instead of a literal Space ():
^(?!(.*?\*){2,}|.*?\s|\*$).*?\*.*$
How it works:
^ # String starts with ...
(?! # DO NOT match if ...
(.*?\*) # Optional Data followed by a *
{2,} # Multiple times (multiple *s)
| # OR
.*?\s # Optional Data followed by whitespace
| # OR
\*$ # * then the end of the string (i.e. whole string is just a *)
)
.*? # Optional Data before *
\* # *
.* # Optional Data after *
$ # ... String ends with
Demo:
strings = [
'test*',
'*_test',
'test*something',
'test',
'*',
'test_**',
'**_test',
'test*something*',
'test *something',
'test *'
]
for (var i = 0; i < strings.length; i++) {
if (strings[i].match(/^(?!(.*?\*){2,}|.*?\s|\*$).*?\*.*$/)) {
document.write(strings[i] + '<br>')
}
}
You can use /^[^*\s]*\*[^*\s]*$/
to check for a string, that contains exactly one asterisk and no spaces. If only a single asterisk is invalid, you should add a lookahead to check for the presence of at leats two characters like /^(?=.{2})[^*\s]*\*[^*\s]*$/
See https://regex101./r/yE5zV2/1 for some samples (go to unit tests)
You can use this :
var regex = /^([^ \*]|\s)*\*{1}([^ \*]|\s)*$/
var str1 = '*abcfkdsjfksdjf';
var str2 = 'abcfkds*jfksdjf';
var str3 = 'abcfkdsjfksdjf*';
var str4 = 'abcfkdsjfksdjf';
var str5 = 'abcf*kdsjfk*sdjf';
console.log(regex.test(str1)); // returns true;
console.log(regex.test(str2)); // returns true;
console.log(regex.test(str3)); // returns true;
console.log(regex.test(str4)); // returns false;
console.log(regex.test(str5)); // returns false;
The conditions hardly require any regex (good to use when checking for whitespace though).
// This is the checking function
function checkStr(x) {
return (x.indexOf("*", x.indexOf("*")+1) === -1 && x.indexOf("*") > -1 && !/\s/.test(x));
}
// And this is the demo code
var valid = ["123*567", "*hereandthere", "noidea*"];
var invalid = ["123*567*", "*here and there", "noidea**", "kkkk"];
valid.map(x => document.body.innerHTML += "<b>" + x + "</b>: " + checkStr(x) + "<br/><b>");
invalid.map(x => document.body.innerHTML += "<b>" + x + "</b>: " + checkStr(x) + "<br/><b>");
The POI is return (x.indexOf("*", x.indexOf("*")+1) === -1 && x.indexOf("*") > -1 && !/\s/.test(x));
:
x.indexOf("*", x.indexOf("*")+1) === -1
- makes sure there are no 2 asterisksx.indexOf("*") > -1
- makes sure there is at least one*
!/\s/.test(x)
- makes sure there is no whitespace in the string.
本文标签: javascriptRegular Expression for a string that should contain only one asterisk (*)Stack Overflow
版权声明:本文标题:javascript - Regular Expression for a string that should contain only one asterisk (*) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744372070a2603078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论