简体   繁体   中英

finding first occurrence of a regex character set

I found many solutions online for regex matching the first occurrence of a string, a certain character, a word, etc, but I have yet to find a solution for matching the first occurrence of a SET of characters (or in my case, NOT matching a set of characters).

eg I have a string as below (in javascript):

var testString = '~!@#$%^&*()_+|}{POIUYTREWQ":?><asdfghjklm,./;[]\=-0987654321`~!@#$%^&*()_+|}{POIUYTREWQ":?><asdfghjklm,./;[]\=-0987654321`~!@#$%^&*()_+|}{POIUYTREWQ":?><asdfghjklm,./;[]\=-0987654321`'

As you can see, there are many, many, many occurrences of weird characters within testString.

I put up a regex match to show me which are the offending characters as below:

var regTest = /[^A-Za-z0-9.,?()@\[\]\-\/ ]/g;
var wrongChar = testString.match(regTest);

Now, my problem is that even though wrongChar nicely returns an array of the non-matched characters, it gives me every occurrence of the characters, as below:

~,!,#,$,%,^,&,*,_,+,|,},{,",:,>,<,;,\,=,`,~,!,#,$,%,^,&,*,_,+,|,},{,",:,>,<,;,\,=,`,~,!,#,$,%,^,&,*,_,+,|,},{,",:,>,<,;,\,=,`

Is there way to give me only the FIRST occurrence of every unwanted character in a quick way(such as a change in my regex), or would I have to create 2 arrays to keep testing if a character has already been saved inside wrongChar(the long method)?

Use the search method:

var testString = '~!@#$%^&*()_+|}{POIUYTREWQ":?><asdfghjklm,./;[]\=-0987654321`~!@#$%^&*()_+|}{POIUYTREWQ":?><asdfghjklm,./;[]\=-0987654321`~!@#$%^&*()_+|}{POIUYTREWQ":?><asdfghjklm,./;[]\=-0987654321`';
var regTest = /[^A-Za-z0-9.,?()@\[\]\-\/ ]/g;
var first = testString.search(regTest);

Notice that if the method can't match the regex you're providing, it will return -1 (this method is similar to indexOf )

To get only one occurence, make the regex non-global.

To get each character only once, just remove duplicates from the wrongChar result array:

var singleChars = wrongChar.sort().reduce(function(res, x) {
    if (x != res[res.length-1])
         res.push(x);
    return res;
}, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM