Regex Tester for JavaScript
Regular Expressions in Java
Java Regex Tutorial by Lars Vogel
Info: JavaScript
1
2
| var matches = json.match(/REGEX/); // Beendet nach dem ersten Fund
var matches = json.match(/REGEX/g); // Durchsucht bis zum Ende |
var matches = json.match(/REGEX/); // Beendet nach dem ersten Fund
var matches = json.match(/REGEX/g); // Durchsucht bis zum Ende
See: Regular Expressions in JavaScript, part 2
i // ignore case // uppercase and lowercase will be ignored
g // global search // the search is carried out across the entire string
m // multiline search // the regular expression will match over multiple lines
Capturing and non-capturing groups
Capturing groups
var string = '1st,2nd,3rd,4th';
var matches = string.match(/([0-9]+)(st|nd|rd|th)/g);
console.log(matches); // ["1st", "2nd", "3rd", "4th"]
var replaces = string.replace(/([0-9]+)(st|nd|rd|th)/g,'$1');
console.log(replaces); // 1,2,3,4 |
var string = '1st,2nd,3rd,4th';
var matches = string.match(/([0-9]+)(st|nd|rd|th)/g);
console.log(matches); // ["1st", "2nd", "3rd", "4th"]
var replaces = string.replace(/([0-9]+)(st|nd|rd|th)/g,'$1');
console.log(replaces); // 1,2,3,4
Die Gruppen (...)
bilden Captures.
([0-9]+)
ist Capture 1 ($1)
(st|nd|rd|th)
ist Capture 2 ($2)
Non-capturing groups
Möchte man ([0-9]+)
vom Capturing ausschließen, so muss man eine non-capturing group daraus machen. Das geht mit (?:[0-9]+)
.
Beispiel:
var string = '1st,2nd,3rd,4th';
var matches = string.match(/([0-9]+)(st|nd|rd|th)/g);
console.log(matches); // ["1st", "2nd", "3rd", "4th"]
var replaces = string.replace(/(?:[0-9]+)(st|nd|rd|th)/g,'$1');
console.log(replaces); // st,nd,rd,th |
var string = '1st,2nd,3rd,4th';
var matches = string.match(/([0-9]+)(st|nd|rd|th)/g);
console.log(matches); // ["1st", "2nd", "3rd", "4th"]
var replaces = string.replace(/(?:[0-9]+)(st|nd|rd|th)/g,'$1');
console.log(replaces); // st,nd,rd,th
Match word in a sentence
var sentence = 'The internet browser Mozilla Firefox awesome.';
var match = sentence.match(/(Firefox)/)[0];
document.write(match); // Firefox |
var sentence = 'The internet browser Mozilla Firefox awesome.';
var match = sentence.match(/(Firefox)/)[0];
document.write(match); // Firefox
Pattern pattern = Pattern.compile("(Firefox)");
Matcher matcher = pattern.matcher("The internet browser Mozilla Firefox awesome.");
matcher.find();
System.out.println(matcher.group(0)); // Firefox |
Pattern pattern = Pattern.compile("(Firefox)");
Matcher matcher = pattern.matcher("The internet browser Mozilla Firefox awesome.");
matcher.find();
System.out.println(matcher.group(0)); // Firefox
Match sentence with two words
var sentence = 'I like the Mozilla Firefox browser.';
var match = sentence.match(/.*(Mozilla).*(Firefox).*/)[0];
document.write(match); // I like the Mozilla Firefox browser. |
var sentence = 'I like the Mozilla Firefox browser.';
var match = sentence.match(/.*(Mozilla).*(Firefox).*/)[0];
document.write(match); // I like the Mozilla Firefox browser.
Pattern pattern = Pattern.compile(".*(Mozilla).*(Firefox).*");
Matcher matcher = pattern.matcher("I like the Mozilla Firefox browser.");
matcher.find();
System.out.println(matcher.group(0)); // I like the Mozilla Firefox browser. |
Pattern pattern = Pattern.compile(".*(Mozilla).*(Firefox).*");
Matcher matcher = pattern.matcher("I like the Mozilla Firefox browser.");
matcher.find();
System.out.println(matcher.group(0)); // I like the Mozilla Firefox browser.
Match everything after the first comma (Explanation)
var sentence = 'Match everything, after the first comma.';
var match = sentence.match(/,.*$/)[0];
document.write(match); // , after the first comma. |
var sentence = 'Match everything, after the first comma.';
var match = sentence.match(/,.*$/)[0];
document.write(match); // , after the first comma.
Pattern pattern = Pattern.compile(",.*$");
Matcher matcher = pattern.matcher("Match everything, after the first comma.");
matcher.find();
System.out.println(matcher.group(0)); // , after the first comma. |
Pattern pattern = Pattern.compile(",.*$");
Matcher matcher = pattern.matcher("Match everything, after the first comma.");
matcher.find();
System.out.println(matcher.group(0)); // , after the first comma.
Match everything after the last comma (Explanation)
var sentence = 'Match everything, after the last comma, hihi.';
var match = sentence.match(/[^,]*$/)[0];
document.write(match); // hihi. |
var sentence = 'Match everything, after the last comma, hihi.';
var match = sentence.match(/[^,]*$/)[0];
document.write(match); // hihi.
Pattern pattern = Pattern.compile("[^,]*$");
Matcher matcher = pattern.matcher("Match everything, after the last comma, hihi.");
matcher.find();
System.out.println(matcher.group(0)); // hihi |
Pattern pattern = Pattern.compile("[^,]*$");
Matcher matcher = pattern.matcher("Match everything, after the last comma, hihi.");
matcher.find();
System.out.println(matcher.group(0)); // hihi
Match everything when it does not contain a certain word (Explanation)
var sentence = 'I like pizza too much.';
var match = sentence.match(/^((?!pizza).)*$/);
document.write(match); // null |
var sentence = 'I like pizza too much.';
var match = sentence.match(/^((?!pizza).)*$/);
document.write(match); // null
Pattern pattern = Pattern.compile("^((?!pizza).)*$");
Matcher matcher = pattern.matcher("I like pizza too much.");
matcher.find();
System.out.println(matcher.group(0)); // java.lang.IllegalStateException: No match found |
Pattern pattern = Pattern.compile("^((?!pizza).)*$");
Matcher matcher = pattern.matcher("I like pizza too much.");
matcher.find();
System.out.println(matcher.group(0)); // java.lang.IllegalStateException: No match found
Match everything after a certain word
var url = 'www.bennyn.de';
var match = url.match(/[^www.].*/)[0];
document.write(match); // bennyn.de |
var url = 'www.bennyn.de';
var match = url.match(/[^www.].*/)[0];
document.write(match); // bennyn.de
Pattern pattern = Pattern.compile("[^www.].*");
Matcher matcher = pattern.matcher("www.bennyn.de");
matcher.find();
System.out.println(matcher.group(0)); // bennyn.de |
Pattern pattern = Pattern.compile("[^www.].*");
Matcher matcher = pattern.matcher("www.bennyn.de");
matcher.find();
System.out.println(matcher.group(0)); // bennyn.de
Match everything until a certain character
var sentence = 'The dot is missing.';
var match = sentence.match(/.*?(?=\.)/)[0];
document.write(match); // The dot is missing |
var sentence = 'The dot is missing.';
var match = sentence.match(/.*?(?=\.)/)[0];
document.write(match); // The dot is missing
Pattern pattern = Pattern.compile(".*?(?=\\.)");
Matcher matcher = pattern.matcher("The dot is missing.");
matcher.find();
System.out.println(matcher.group(0)); // The dot is missing |
Pattern pattern = Pattern.compile(".*?(?=\\.)");
Matcher matcher = pattern.matcher("The dot is missing.");
matcher.find();
System.out.println(matcher.group(0)); // The dot is missing
Match domain in an URL
var url = 'http://angelcode.de:3000/room/e0e669ac5fe7ca5';
var match = url.match(/[^http:\/\/].*?(?=:)/)[0];
document.write(match); // angelcode.de |
var url = 'http://angelcode.de:3000/room/e0e669ac5fe7ca5';
var match = url.match(/[^http:\/\/].*?(?=:)/)[0];
document.write(match); // angelcode.de
Pattern pattern = Pattern.compile("[^http:\\/\\/].*?(?=:)");
Matcher matcher = pattern.matcher("http://angelcode.de:3000/room/e0e669ac5fe7ca5");
matcher.find();
System.out.println(matcher.group(0)); // angelcode.de |
Pattern pattern = Pattern.compile("[^http:\\/\\/].*?(?=:)");
Matcher matcher = pattern.matcher("http://angelcode.de:3000/room/e0e669ac5fe7ca5");
matcher.find();
System.out.println(matcher.group(0)); // angelcode.de
Match arbitrary ending / match everything that begins with /
const url = "https://api.binance.com/api/v3/account?timestamp=1520550477929&signature=123"
const pattern = /https:\/\/api\.binance\.com\/api\/v3\/account.*$/
const matching = url.match(pattern)[0] |
const url = "https://api.binance.com/api/v3/account?timestamp=1520550477929&signature=123"
const pattern = /https:\/\/api\.binance\.com\/api\/v3\/account.*$/
const matching = url.match(pattern)[0]