Reputation: 5637
Is there a way to allow a certain range of characters, but also exclude certain patterns of these allowed characters?
For example, let's say you're searching for a range of letters:
[A-Za-z]
but you don't want to match a certain pattern of these accepted characters, for example:
abc
I've been trying to craft an expression for a while and have had no luck. What I've been able to come up with was:
([A-Za-z][^abc])*
But this doesn't provide the behavior I'm looking for. Is there a way to search a range of characters and exclude certain patters of the accepted characters?
EDIT
To clarify, I was wondering if I could be able to still match the characters besides the unaccepted pattern. For example, if we had:
SomeTextabcMoreText
and abc
is an illegal pattern, is there a way to match the legal characters and yield some result similar to:
SomeText
MoreText
Upvotes: 0
Views: 56
Reputation: 1159
I don't think that regexes are the right solution to this.
There are two option depending upon language:
String.replace
- If you're looking to just output the results.String.split
if you're looking to tokenize the results.$('#replace').text("SomeabcStringabcwithsomeabccharacters".replace(/abc/g, ""));
$('#split').text(JSON.stringify("SomeabcStringabcwithsomeabccharacters".split("abc")));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='replace'></div>
<div id='split'></div>
Upvotes: 1
Reputation: 5637
After taking a step back and analyzing my particular problem more, I realize that Regex wouldn't be the best way to solve this.
It would make more sense to parse the string using the illegal pattern as a delimiter:
var data = 'SomeTextabcMoreText';
var illegalPattern = 'abc';
console.log(data.split(illegalPattern)); // => ["SomeText", "MoreText"]
Upvotes: 0