RegEx experts - string help

RegEx experts - string help
0.0 0

#1

I want to search a string for duplicates and replace the duplicate characters with a new character,
if not a duplicate (unique), I want to replace that character with a different character. I’m at a loss. I know I can search for specific characters, but not sure how to search for duplicates using RegEx


#2

Oh dang I just spent all last night trying to learn that stuff.

/([A-Z])\1/gi

then replace whatever in the parentheses… I think


#3

Check out https://regex101.com/ …was super helpful for me when trying to create RegEx for the phone number validation challenge. Make sure to switch to whichever language you’re using on the left side just to be consistent.

You could also do this by splitting your string into an array of characters, removing/replacing duplicates in the array, then joining back into a string.


#4

You need to be more specific - duplicate as in two same characters in a row or two same characters in a string/word?


#5

I found https://regexone.com to be a really good tutorial for the basics of regex. It’s not language specific though.


#6

This is a resource I use as a reference and to test RegEx patterns.

http://regexr.com/


#7

that was a great start, thanks


#8

Awesome site, even has definitions to check and explains what my regex is doing


#9

here’s what I got -

/(\^{2,})/gi

(\^{2,}) - Capturing Group

\^{2,} - matches the character " ^ " literally (case insensitive) "this matches only if the ^ is in a sequence; like ^^ , but No Match with; ^k^

{2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy)

Global pattern flags;
g modifier: global. All matches (don’t return after first match)

i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

this is my TEST string;

(")()(&^@$^%^$K&^!a)()")

#10

THIS WORKS to identify if it’s present, but not how many occurrences - gonna have to count occurrences and run some other stuff to get what I want, was hoping to count as well

/([\^])/g

on this is TEST string;

(")()(&^@$^%^$K&^!a)()")

thanks @sebalex11, @jamesperrin, @the-thief and @evanfreeze


#12

this FCC regex challenge is what I needed -

// Setup
var testString = "How many ()non-space ch^ara)cters a^re there in th)is senten^ce?";

// Only change code below this line.

var expression = /.\^/g;  // Change this line

// Only change code above this line

// This code counts the matches of expression in testString
var nonSpaceCount = testString.match(expression).length;

#13

@EgoDominusVos Not sure if you are looking for the right thing then.

They want you to look for non-space characters. So letters, numbers and special characters. In other words, any other then a space, tab or new line/carriage return.

\s is used to find space characters.


#14

SPOILER BELOW

… and \S for non-whitespace characters.


#15

I was trying to give a hint with out giving away the answer.


#16

Thanks for the heads-up! I’ve blurred the spoiler now.


#17

people, people, people …

I’m WAY past that in FCC, currently working in Advanced Front End Development Projects …


#18

Are you looking for caret ^ or non-space characters?


#20

@the-thief , I’m using the code from the example as a reference point for one of the advanced projects. I’m not using it for the challenge, passed that long ago.

@Soupedenuit , not sure why the snarky … I couldn’t find anything even remotely upsetting in my responses to this topic, in fact, I’ve thrown out positive vibes only … answering questions and supplying add’l info as requested. Glad you withdrew your comment; much peace, love and all that kumb(yeah) fireside lovin’ stuff…


#21

Return value

An Array containing the entire match result and any parentheses-captured matched results; null if there were no matches.

You can use the length attribute of the returned array to figure out how many occurrences of your target are present in the string.


#22

ahhh, that’s the ticket, thanks :thumbsup: