Regex to find certain characters in a string

regex match exactly 3 characters
regex match two specific characters
regex match string
regex match exact string
regex contains string
regex any character
regular expression
regex special characters

I'm doing this in Javascript, so there might be an easier way? However I have a string that looks like this:


An example of this would be:


What I want to do is to check to see if this string ends with icon_<random_id>.png If it does, do X, otherwise do nothing

I'm not very good at Regex. I've tried ^icon\_{*}\.png$ but I'm not having a lot of joy

You can use this code to simple check is the partial exist or not :

let reg = /(icon_[\S]+\.png)$/iu; 

let str = 'designs/DMFSx881yveor8qolW7DUuPxWDQ2/e53BqQG2Y3wnn8uVRw8m/icon_a5898064.png';

let check = str.match(reg);

if(Array.isArray(check) && check[0]) {
console.log('not exists');

Regular Expressions, Character classes — \d \w \s and .​​ \d matches a single character that is a digit -> Try it! \w matches a word character (alphanumeric character plus underscore) -> Try it! \s matches a whitespace character (includes tabs and line breaks). matches any character -> Try it! A regular expression is a form of advanced searching that looks for specific patterns, as opposed to certain terms and phrases. With RegEx you can use pattern matching to search for particular strings of characters rather than constructing multiple, literal search queries. RegEx uses metacharacters in conjunction with a search engine to retrieve specific patterns. Metacharacters are the building blocks of regular expressions.

That one matches your use case :


Then you can do something like :

var matches = str.match(/\/icon_(\w+)\.png$/g);

Regular expressions quick reference, These symbols indicate the start and the end of a string, respectively: a string that starts and ends with "abc" - effectively an exact match comparison. bounds, which appear inside braces and indicate ranges in the number of occurrences:  The System.Text.RegularExpressions.Regex class can be used to search strings. These searches can range in complexity from simple to complicated text patterns. The following code example searches for the word "the" or "their" in a sentence, ignoring case. The static method Regex.IsMatch performs the search. You give it the string to search and a

I would think something like /(icon_[A-z0-9]+.png)$/might be what you're looking for.

var img-reg = /(icon_[A-z0-9]+.png)$/;
var url = 'designs/DMFSx8uPxWDQ2/e53BqQG2Y3wnn8uVRw8m/icon_a58598064.png'; 
if (img-reg.test(url)) {
    //do something because it matched.

icon_ + any amount of numbers or letters followed by .png and the $ represents that it has to be at the end of the string. when you use a ^ at the beginning you state that the start has to match too.

Regex tutorial, A common file processing requirement is to match strings within the file to a standard At their simplest, a regular expression is simply a string of characters and this For this to be possible we need to define some syntax that lets us specify  In regex, we can match any character using period "." character. To match only a given set of characters, we should use character classes. 1. Match any character using regex '.' character will match any character without regard to what character it is. The matched character can be an alphabet, number of any special character.

This regex should work for your purpose:


Simple RegEx Tutorial, You can use this code to simple check is the partial exist or not : let reg = /(icon_[\​S]+\.png)$/iu; let str  There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else. Below are a couple lines, where we only want to match the first three strings, but not the last three strings.

if x = 'your string'
y = x.match(/\/icon_[^/]+.png/);

if this return value to y then x string you needed

Pattern Matching With Regular Expressions, Some punctuations are specially defined in regular expression. To match these characters in string, add "\" in pattern. For example:  The tables are meant to serve as an accelerated regex course, and they are meant to be read slowly, one line at a time. On each line, in the leftmost column, you will find a new element of regex syntax. The next column, "Legend", explains what the element means (or encodes) in the regex syntax.

Regex to find certain characters in a string, For example, “\d” in a regular expression is a metacharacter that represents a digit character, but “d” stands for the literal character, “d.” You can use regular expressions to search for social security numbers, patent numbers, URLs, email addresses, Bates numbers, and other strings that follow a specific pattern. Regular expression is used to : (1) test a string whether it matches a pattern, such as a email address. (2) to find a substring which matches certain pattern, from a whole text. (3) to do complex replacement in a text. It is very simple to study regular expression syntax, and the few abstract concepts can be understood easily too.

Regular Expression Syntax Reference, opposed to certain terms and phrases. With RegEx you can use pattern matching to search for particular strings of characters rather than constructing multiple,  With the help of my colleague,we came out with a simple regex to find the occurrence of exact word in a long string, even if the word is surrounded by comma, full-stop or some other character. The regex is:- String matchingWord="string" // word to find String longString="it is a very long string. find the exact word in it" // method to return

Searching with regular expressions, We will get to that later. This regex can match the second a too. It only does so when you tell the regex engine to start searching through the string after the first 

  • What have you tried so far?
  • @UllasHunka indexOf won't work with a random_id
  • Added in what I have tried, and continuing to try
  • what can the randomId consists of? your regex accepts only numbers but the example also has the letter "a" in there
  • I've just edited it to ^icon\_{*}\.png$ The random string is made up of alphanumeric character including lower and upper case
  • This one almost works for my use case. The random string after icon_ can be anything acceptable and it doesn't appear to work for all characters
  • you need to escape _ and .
  • Thanks for pointing that out I'll edit the answer. This regex worked without the escaped characters on for some reason.