This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 4 years ago.
How to replace a string between two string in javascript
StartLine = `/*TESTSTART*/`;
Endline = `/*TESTEND*/`;
OriginalContent = `/*TESTSTART*/
testing
not
working
/*TESTEND*/`;
var e = OriginalContent .replace(/(StartLine)[\s\S]*?(Endline)/,' it's
working
fine');
OUTPUT = `/*TESTSTART*/
it's
working
fine
/*TESTEND*/`
1) How to check if the string contains / in regular exp?
2) if I stored sting in one variable, how can I use this variable in regular exp?
You can escape a / character with a backslash \ if you're using / to start your regular expression. But in this case, since you want to include the value of a variable in your regular expression, you should use a string to represent a regex instead, in which case there is no need to escape / but you should escape other special regex characters such as * with two backslashes, and you can simply concatenate the variable with the other string literals and variables to form the full regex:
StartLine = '/\\*TESTSTART\\*/';
Endline = '/\\*TESTEND\\*/';
...
var e = OriginalContent.replace(StartLine + '[\s\S]*?' + Endline, "it's
working
fine");
Related
I have
Pattern pattern = r'^((?:19|20)\d\d)[- /.]
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$';
My editor shows an error on this regexp:
How can I fix it?
You entered a line break inside a string literal, that is why you get a syntax issue.
If you want to split a pattern into several lines, just use string concatenation:
Pattern pattern = r'^((?:19|20)\d\d)[- /.]' +
r'(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$';
Or, since string literals separated only with whitespace characters are concatenated automatically:
Pattern pattern = r'^((?:19|20)\d\d)[- /.]'
r'(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$';
Or, if you plan to re-use a long pattern, you may define this part as a variable, and just use string interpolation:
String d = r'((?:19|20)\d\d)';
String M = r'(0[1-9]|1[012])';
String y = r'(0[1-9]|[12][0-9]|3[01])';
String sep = r'[- /.]';
Pattern pattern = '^$d$sep$M$sep$y\$';
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 5 years ago.
I am trying to use the new \R regex matcher from java 8.
However, for the following code :
public static void main(String[] args)
{
String s = "This is \r\n a String with \n Different Newlines \r and other things.";
System.out.println(s);
System.out.println(Pattern.matches("\\R", s));
if (Pattern.matches("\\R", s)) // <-- is always false
{
System.out.println("Matched");
}
System.out.println(s.replaceAll("\\R", "<br/>")); //This is a String with <br/> Different Newlines <br/> and other things.
}
The Pattern.matches always returns false, where as the replaceAll method does seem to find a match and does what I want it to. How do I make the Pattern.matches work ?
I have also tried the long about way and still can't get it to work :
Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.matches();
System.out.println(b);
Well matches (both in String and Matchers classes) attempts to match the complete input string.
You need to use matcher.find instead:
Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.find();
System.out.println(b);
From Java docs:
\R Matches any Unicode line-break sequence, that is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
PS; If you want to know if input contains a line-break then this one liner will work for you:
boolean b = s.matches("(?s).*?\\R.*");
Note use of .* on either side of \R to make sure we are matching complete input. Also you need (?s) to enable DOTALL mode to be able to match multiline string with .*
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I am looking for regular expression for exact letters in middle of word. which should match Dot(.) also.
Currently I am using regular expression is "\\w*."+inputString +"\\w*", "i" , actually period represents any letter in this expression.
eg:
inputData = {name:[abc.12, abcdef, bc1454, test, rahul, bc.reju, rewbc.]}
inputString = "bc."
var wordFormat = new RegExp('\\w*'+inputString +'\\w*', 'i');
workFormat.test(inputData);
scenario 1: Starting of word.
input : 'bc.'
actual output is: abc.12
expecting output is: abc.12, bc.reju, rewbc.
expect output should get only one because passing inputString matches only one item in array of object (inputData) so, expecting output item is 1.
Here is a demo - Regex101
You can modify this by replacing the "bc" with your search string.
\w*bc\w*\.\d*
updated Expression
\w*bc\.\d*
Here is the example that you can use as your requirement:
regular = "bc." //These was the actual input expression you want to test
var regularplacholder = '[character]';
var regularexpresion="/";
for(var index =0;index<regular.length;index++)
{
regularexpresion+=(regularplacholder.replace('character',regular[index]));
}
regularexpresion+='*'
if(regularexpresion.test('test string')){
//your logic here
}
I need to check strings like *11F23H3* that start and end with a *and have 7 uppercase literals or numbers in between. So far I have:
if (!barcode.match('[*A-Z0-9*]')) {
console.error(`ERROR: Barcode not valid`);
process.exitCode = 1;
}
But this does not cover strings like *11111111111*. How would the correct regex look like?
I need to check strings like 11F23H3 that start and end with a *and have 7 uppercase literals or numbers in between
You can use this regex:
/\*[A-Z0-9]{7}\*/
* is regex meta character that needs to be escaped outside character class
[A-Z0-9]{7} will match 7 characters containing uppercase letter or digits
RegEx Demo
Code:
var re = /\*[A-Z0-9]{7}\*/;
if (!re.test(barcode)) {
console.error(`ERROR: Barcode ${barcode} in row ${row} is not valid`);
process.exitCode = 1;
}
Note that if barcode is only going to have this string then you should also use anchors like this to avoid matching any other text on either side of *:
var re = /^\*[A-Z0-9]{7}\*$/;
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
/route/some pic name.jpg
I need to find all white spaces beetwen route/ and .jpg using RexExp.
You can just use this regular expression selector. Just make sure to use the global flag (/g):
\s
Here is an example:
var text = "/route/some pic name.jpg";
var regex = /\s/g;
var replacement = "_";
var result = text.replace(regex, replacement);
console.log(result);
If you want to match only spaces, you can also just use a space as a selector together with the global flag /g.
Regexp:
\s <= 1 White Space
\s+ <= 1 or more White Space
\s* <= 0 or more White Space
const regex = /\s+/g;
const str = `/route/some pic name.jpg`;
const subst = `-`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
You just need \s like /\s/g
/g will match all ocurrences of \s
If you want to replace spaces with say "_" or if you want to count the number of spaces in the string, you can do something like this.
String pattern = "\\s+"; // regular expression for whitespaces
String str = "/route/some pic name.jpg";
System.out.println(str.replaceAll(pattern, "_"));
System.out.println(str.length() - str.replaceAll(pattern, "").length()); // prints 2
The above code snippet is in Java.