regex javas code example

Example 1: java regex

import java.util.regex.*;  
public class RegexExample1{  
public static void main(String args[]){  
//1st way  
Pattern p = Pattern.compile(".s");//. represents single character  
Matcher m = p.matcher("as");  
boolean b = m.matches();  
  
//2nd way  
boolean b2=Pattern.compile(".s").matcher("as").matches();  
  
//3rd way  
boolean b3 = Pattern.matches(".s", "as");  
  
System.out.println(b+" "+b2+" "+b3);  
}}

Example 2: using regex in javascript

//Adding '/' around regex
var regex = /\s/g;
//or using RegExp
var regex = new RegExp("\s", "g");

Example 3: regex for strings with specific letters javascript

let re = /ab+c/;

Example 4: regular expression javascript

// Tests website Regular Expression against document.location (current page url)
if (/^https\:\/\/example\.com\/$/.exec(document.location)){
	console.log("Look mam, I can regex!");
}

Example 5: ("\\s","")regex java

x.replaceAll("\\s+", "");