JS Regexp - how to find text in a string - javascript

There is some text, exp: "The string class is an instantiation of the basic_string class template that uses char".
I need to find the text - "basic_string", but if there is no word "the" in front of him.
If use negative lookbehind, it was be:
(?<!\sthe)\s+basic_string
But javascript not understand negative lookbehind, what to do?

If the only allowed character between "the" and "basic_string" is the white-space:
([^e\s]|[^h]e|[^t]he)\s+basic_string

You can use xregexp library to get advanced regex features like lookbehind in Javascript.
Alternatively you can use alternation and capture group as a workaround:
var s = 'The string class is an instantiation of the basic_string class template that uses char';
var kw = s.match(/\bthe basic_string\b|(\bbasic_string\b)/)[1];
// undefined
s = 'instantiation of basic_string class template'
kw = s.match(/\bthe basic_string\b|(\bbasic_string\b)/)[1]
//=> "basic_string"
In this regex, captured group #1 will only be populated if bbasic_string isn't preceded by word the.

You can use RegExp /(the)(?\sbasic_string)/ or new RegExp("(" + before + ")(?=" + match + ")") to match "the" if followed by " basic_string", .match() to retrieve .index of matched string, .slice() to get "basic_string"
var str = "The string class is an instantiation of the basic_string class template that uses char";
var before = "the";
var match = " basic_string";
var index = str.match(new RegExp("(" + before + ")(?=" + match + ")")).index
+ before.length + 1;
console.log(str.slice(index, index + match.length));

The easiest way to emulate the negative lookbehind is via an optional capturing group, and check if the group participated in the match:
/(\bthe)?\s+basic_string/g
^^^^^^^^
See this JS demo:
var s = 'The string class is an instantiation of the basic_string class template that uses char, not basic_string.';
var re = /(\bthe)?(\s+basic_string)/gi;
var res = s.replace(re, function(match, group1, group2) {
return group1 ? match : "<b>" + group2 + "</b>";
});
document.body.innerHTML = res;

Related

Regular Expression to match compound words using only the first word

I am trying to create a regular expression in JS which will match the occurences of box and return the full compound word
Using the string:
the box which is contained within a box-wrap has a box-button
I would like to get:
[box, box-wrap, box-button]
Is this possible to match these words only using the string box?
This is what I have tried so far but it does not return the results I desire.
http://jsfiddle.net/w860xdme/
var str ='the box which is contained within a box-wrap has a box-button';
var regex = new RegExp('([\w-]*box[\w-]*)', 'g');
document.getElementById('output').innerHTML=str.match(regex);
Try this way:
([\w-]*box[\w-]*)
Regex live here.
Requested by comments, here is a working example in javascript:
function my_search(word, sentence) {
var pattern = new RegExp("([\\w-]*" + word + "[\\w-]*)", "gi");
sentence.replace(pattern, function(match) {
document.write(match + "<br>"); // here you can do what do you want
return match;
});
};
var phrase = "the box which is contained within a box-wrap " +
"has a box-button. it is inbox...";
my_search("box", phrase);
Hope it helps.
I'll just throw this out there:
(box[\w-]*)+
You can use this regex in JS:
var w = "box"
var re = new RegExp("\\b" + w + "\\S*");
RegEx Demo
This should work, note the 'W' is upper case.
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
\Wbox\W
It looks like you're wanting to use the match with a regex. Match is a string method that will take a regex as an argument and return an array containing matches.
var str = "your string that contains all of the words you're looking for";
var regex = /you(\S)*(?=\s)/g;
var returnedArray = str.match(regex);
//console.log(returnedArray) returns ['you', 'you\'re']

Capturing String Segments between Special Characters using Regular Expressions

I have the following string of text:
textString1:textString2:textString3:textString4
I'm looking to capture each text string and assign them to variables.
I've somehow managed to come up with the following:
var errorText = 'AAAA:BBBB:CCCC:DDDD';
var subString, intro, host, priority, queue = '';
var re = /(.+?\:)/g;
subString = errorText.match(re);
intro = subString[0];
host = subString[1];
priority = subString[2];
//queue = subString[3];
console.log(intro + " " + host + " " + priority);
JS Bin Link
However, I'm having problems with:
capturing the last group, since there is no : at the end
the variables contain : which I'd like to strip
You don't need a regex for this - just use errorText.split(':') to split by a colon. It will return an array.
And if you then want to add them together with spaces, you could do a simple replace instead: errorText.replace(/:/g,' ').
use split method for this.it will return array of string then iterate through array to get string:
var errorText = 'AAAA:BBBB:CCCC:DDDD';
var strArr=errorText.split(':');
console.log(errorText.split(':'));
for(key in strArr){
console.log(strArr[key]);
}

Regex to get word started with # in javascript

I have a problem replace certain words started with #. I have the following code
var x="#google",
eval("var pattern = /" + '\\b' + x + '\\b');
txt.replace(pattern,"MyNewWord");
when I use the following code it works fine
var x="google",
eval("var pattern = /" + '\\b' + x + '\\b');
txt.replace(pattern,"MyNewWord");
it works fine
any suggestion how to make the first part of code working
ps. I use eval because x will be a user input.
The problem is that \b represents a boundary between a "word" character (letter, digit, or underscore) and a "non-word" character (anything else). # is a non-word character, so \b# means "a # that is preceded by a word character" — which is not at all what you want. If anything, you want something more like \B#; \B is a non-boundary, so \B# means "a # that is not preceded by a word character".
I'm guessing that you want your words to be separated by whitespace, instead of by a programming-language concept of what makes something a "word" character or a "non-word" character; for that, you could write:
var x = '#google'; // or 'google'
var pattern = new RegExp('(^|\\s)' + x);
var result = txt.replace(pattern, '$1' + 'MyNewWord');
Edited to add: If x is really supposed to be a literal string, not a regex at all, then you should "quote" all of the special characters in it, with a backslash. You can do that by writing this:
var x = '#google'; // or 'google' or '$google' or whatever
var quotedX = x.replace(/[^\w\s]/g, '\\$&');
var pattern = new RegExp('(^|\\s)' + quotedX);
var result = txt.replace(pattern, '$1' + 'MyNewWord');
Make you patter something like this:
/(#)?\w*/
If you want to make a Regular Expression, try this instead of eval:
var pattern = new RegExp(x);
Btw the line:
eval("var pattern = /" + '\\b' + x + '\\b');
will make an error because of no enclose pattern, should be :
eval("var pattern = /" + '\\b' + x + '\\b/');
How about
var x = "#google";
x.match(/^\#/);

javascript find and replace a dynamic pattern in a string

I have a dynamic pattern that I have been using the code below to find
var matcher = new RegExp("%" + dynamicnumber + ":", "g");
var found = matcher.test(textinput);
I need the pattern to have a new requirement, which is to include an additional trailing 5 characters of either y or n. And then delete it or replace it with a '' (nothing).
I tried this syntax for the pattern, but obviously it does not work.
var matcher = new RegExp("%" + dynamicnumber + ":" + /([yn]{5})/, "g");
Any tip is appreciated
TIA.
You should only pass the regex string into the RegExp c'tor :
var re = new RegExp("%" + number + ":" + "([yn]{5})", "g");
var matcher = new RegExp("(%" + number + ":)([yn]{5})", "g");
Then replace it with the contents of the first capture group.
Use quotes instead of slashes:
var matcher = new RegExp("%" + number + ":([yn]{5})", "g");
Also, make sure that dynamicnumber or number are valid RegExps. special characters have to be prefixed by a double slash, \\, a literal double slash has to be written as four slashes: \\\\.

Javascript regular expression is failing

I'm using the following javascript regex:
var pattern = new RegExp("^" + term + ".*")
console.log(pattern.toSource());
console.log(first_name + " : " + pattern.test(first_name) );
All i want it to do is check if the first name of the person begins with the search term given. E.g if the search term is 'a', then all the first names starting with a, e.g: andy, alice, etc should match. If its al, then only alice should match, etc. However the output is:
/^a.*/
Alyssa : false
What am I doing wrong?
Regexs are case-sensitive. So Alyssa won't match, because A and a are different symbols.
You may want to use case-insensitive regex match:
var pattern = new RegExp("^" + term + ".*", "i")
console.log(pattern.toSource());
console.log(first_name + " : " + pattern.test(first_name) );
There is nothing wrong, you should make the RegExp case insensitive using
var pattern = new RegExp("^" + term + ".*","i")
to match your name as long you want to render the test case insensitive or use a match like /^[aA].*/
You could specify your RegEx to be case insensitive, so that a will match both a and A:
var pattern = new RegExp("^" + term + ".*", "i");
Make your RegExp case insensitive:
var pattern = new RegExp("^" + term + ".*", "i");
See the documentation.
your regex is case sensitive so it's not matching Alyssa add this line to your code:
pattern.ignoreCase = true;
The reason it isn't working is that regular expressions are case sensitive. A is not a
Other things you are doing wrong:
Bothering to say "Followed by zero or more characters", that is just redundant.
Using a regular expression when a simple substring check will do the job.
I'd do it like this:
console.log(
first_name + " : " +
(first_name.toLowerCase().indexOf(term.toLowerCase()) === 0)
);

Categories