How to convert Java Regex to JavaScript regex? - javascript

How to convert regex java in regex javascript
I have for example the text :
#hello some text #Home
My Java regex is
String regex = "[#]+[A-Za-z0-9-_]+\\b";
Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
the result is
#hello and #Home
My Javascript code is :
var myRegExp = new RegExp("[#]+[A-Za-z0-9-_]+\\b");
var tagMatcher = text.match(myRegExp);
but the result is :
#hello
How can I solve the problem?
Where is my error?

Missing global flag g to get the whole set of matches.
var text = "#hello some text #Home";
var myRegExp = new RegExp("[#]+[A-Za-z0-9-_]+\\b", "g");
var tagMatcher = text.match(myRegExp);
console.log(tagMatcher)
Like #JordanRunning mentioned, you can use Regex literal as follow, as well as a shorter approach:
var text = "#hello some text #Home";
var tagMatcher = text.match(/#+[\w-]+\b/g);
console.log(tagMatcher)
Advanced searching with flags
Regular expressions have five optional flags that allow for global and case insensitive searching. These flags can be used separately or together in any order, and are included as part of the regular expression.

Related

javascript regex to find only numbers with hyphen from a string content

In Javascript, from a string like this, I am trying to extract only the number with a hyphen. i.e. 67-64-1 and 35554-44-04. Sometimes there could be more hyphens.
The solvent 67-64-1 is not compatible with 35554-44-04
I tried different regex but not able to get it correctly. For example, this regex gets only the first value.
var msg = 'The solvent 67-64-1 is not compatible with 35554-44-04';
//var regex = /\d+\-?/;
var regex = /(?:\d*-\d*-\d*)/;
var res = msg.match(regex);
console.log(res);
You just need to add the g (global) flag to your regex to match more than once in the string. Note that you should use \d+, not \d*, so that you don't match something like '3--4'. To allow for processing numbers with more hyphens, we use a repeating -\d+ group after the first \d+:
var msg = 'The solvent 67-64-1 is not compatible with 23-35554-44-04 but is compatible with 1-23';
var regex = /\d+(?:-\d+)+/g;
var res = msg.match(regex);
console.log(res);
It gives only first because regex work for first element to test
// g give globel access to find all
var regex = /(?:\d*-\d*-\d*)/g;

Using search and replace with regex in javascript

I have a regular expression that I have been using in notepad++ for search&replace to manipulate some text, and I want to incorporate it into my javascript code. This is the regular expression:
Search
(?-s)(.{150,250}\.(\[\d+\])*)\h+ and replace with \1\r\n\x20\x20\x20
In essence creating new paragraphs for every 150-250 words and indenting them.
This is what I have tried in JavaScript. For a text area <textarea name="textarea1" id="textarea1"></textarea>in the HTML. I have the following JavaScript:
function rep1() {
var re1 = new RegExp('(?-s)(.{150,250}\.(\[\d+\])*)\h+');
var re2 = new RegExp('\1\r\n\x20\x20\x20');
var s = document.getElementById("textarea1").value;
s = string.replace(re1, re2);
document.getElementById("textarea1").value = s;
}
I have also tried placing the regular expressions directly as arguments for string.replace() but that doesn't work either. Any ideas what I'm doing wrong?
Several issues:
JavaScript does not support (?-s). You would need to add modifiers separately. However, this is the default setting in JavaScript, so you can just leave it out. If it was your intention to let . also match line breaks, then use [^] instead of . in JavaScript regexes.
JavaScript does not support \h -- the horizontal white space. Instead you could use [^\S\r\n].
When passing a string literal to new RegExp be aware that backslashes are escape characters for the string literal notation, so they will not end up in the regex. So either double them, or else use JavaScript's regex literal notation
In JavaScript replace will only replace the first occurrence unless you provided the g modifier to the regular expression.
The replacement (second argument to replace) should not be a regex. It should be a string, so don't apply new RegExp to it.
The backreferences in the replacement string should be of the $1 format. JavaScript does not support \1 there.
You reference string where you really want to reference s.
This should work:
function rep1() {
var re1 = /(.{150,250}\.(\[\d+\])*)[^\S\r\n]+/g;
var re2 = '$1\r\n\x20\x20\x20';
var s = document.getElementById("textarea1").value;
s = s.replace(re1, re2);
document.getElementById("textarea1").value = s;
}

Java Script - Regular Expression matching a word in a string

I am trying to find a match in a string with JavaScript. I want to work with the RegEx function.
My example (what I have tried):
var str = "hello.you";
var patt1 = '\\b' + str + '\\b';
var result = str.match(patt1);
But this does not give me the result which I except. I just want to print "you".
Thanks all in advance.
So you jumped right into a pretty advanced regex topic. You sort of want to do a lookahead (the word AFTER a given boundary character). The following will get you there:
let str = "hello.you",
myRegex = /(?<=\.)\w+/;
let theWord = str.match(myRegex);
console.log(theWord[0]);
... And what that does, is uses (?<=.) to indicate "something that comes after a period", followed by \w+ to indicate a word.
I'd recommend using a regex tester, and build from that. I use https://www.regextester.com/

Regex in JavaScript is not matching a string?

I have this JavaScript code:
var textareas = document.getElementsByTagName('textarea');
var content = textareas[0].value;
var reg = new RegExp(/^.*[[]#.+#[]].*$/mgi);
var res = content.match(reg); // always null
The content var contains a long multiline string that contains patterns like [#some text goes here#]. I tested the regex with some online testing tools and it works against the string. Using the regex in JavaScript fails though - any idea why?
Thanks!
How about this?
var content = 'foo\nhead [#some text goes here#] tail\nbar';
var reg = new RegExp(/\[#.+#\]/mgi);
var res = content.match(reg);
On execution, res contains the string '[#some text goes here#]'.
Note that I have escaped [ and ]. If they are not escaped, anything enclosed within them forms a character class.
You used [[] to escape [, which is fine, but you can't use []] to escape ] because it the first ] ends the character class in the regex. This works fine:
/^.*\[#.+#\].*$/mgi
In the case that you only want the single block and not the entire line, use:
/\[#.+#\]/mgi
This should capture the text between hashes (e.g., "some text here"):
var reg = /[^\[]*\[#([^\#]+)#\]/gims

regular expression to remove comment javascript

I am using below regular expression to remove comments from string
<\!{1}\-{2}(.*?)\-{2}\s*>
This is working fine except for mult-iline string
var search = '<\!{1}\-{2}(.*?)\-{2}\s*>';
var re = new RegExp(search, "gm");
var subject = <multi-line string>;
result = subject.replace(re, '');
what should I do to get it working with multiline strings
. does not allow linebreaks.
This one should work:
^(<\!\-{2})((.|\s)*?)\-{2}>$
Fix:
<!--[\S\s]*?-->
I removed the \s at the beginning and the end of the expression and added it in the middle so multiline-comments are allowed.
But you shoud have a look at BartKs comment ;)
regards

Categories

Resources