Regex to Match Phrase ending with a question mark - javascript

I'm trying to figure out a javascript regex that'll match an exact phrase that ends with a question mark, but isn't wrapped in quotes. So far I have this, which matches the phrase "some phrase", but I can't figure out how to match "some phrase?". Any help would be greatly appreciated.
(?<!"|')\some phrase\b(?!"|')

Lookbehinds don't exist in JavaScript. Use the following pattern:(?:[^"']|^)(some phrase\?)(?!["']). [^"']|^ means: any non-quote character or the beginning of a string.
Example:
var text = "....";
var pattern = /(?:[^"']|^)(some phrase\?)(?!["'])/;
var string = text.match(pattern);
var desiredString = string[1]; //Get the grouped text
var patternWithNQuoteGrouped = /([^"']|^)(some phrase\?)(?!["'])/;//Notice: No ?:
var replaceString = text.replace(patternWithNQuoteGrouped, '$1$2');
//$1 = non-quote character $2 = matched phrase
The parentheses around the phrase mark a referable group. (?: means: Create a group, but dereference it. To refer back to it, see the example code. Because lookbehinds don't exist in JavaScript, it's not possible to create a pattern which checks whether a prefix does not exist.

Try this:
var expr = /(^|(?!["']).)(some phrase\?)($|(?!["']).)/;
if (expr.test(searchText)) {
var matchingPhrase = RegExp.$2;
}
http://jsfiddle.net/gilly3/zCUsg/

Related

Regex match string which does not fully consist of angular expressions

I've the pleasure to find all strings in our projects which are not angularjs expressions because we're going multi language (so every string which is not fully between curly braces).
What I wanna do is build a regex which matches all strings, which have no angular expressions (or part of the string is no angular expression).
The var names describe which should match (yes) and which shouldn't (nope).
var yes = "test";
var nope = "{{xyz}}";
var yes = "test {{xyz}}";
var nope = "{{::xyz}}";
var nope = "{{xyz}} {{abc}}"; //as whitespace is okay
Tried a lot of different stuff using negative lookaheads etc. but ended up with a not even close working regex.
"([^}}])+{{|"$
Maybe somebody can help me, as my head is like to explode...
Regex101: https://regex101.com/r/VePtVp/1
You may use following regex for match:
/"(?:\s*{{[^\s}]*}}\s*)+"/
RegEx Demo
RegEx Details:
": Match start quote
(?:: Start non-capture group
\s*{{[^\s}]*}}\s*: Match {{...}} string surrounded by optional whitespaces.
)+: End non-capture group. + matches 1 or more of this group
": Match end quote
Thanks to anubhava as his post and explanation helped me creating this regex here:
"(?!(\{\{[^\s]*\}\}\s*)+").*"
var yes = "test";
var nope = "{{xyz}}";
var yes = "test {{xyz}}";
var yes = "{test";
var yes = "{test} bearbeiten";
var yes = "{test}";
var nope = "{{::xyz}}";
var nope = "{{xyz}} {{abc}}";
var yes = "{{xyz}} test {{abc}}";
var yes = "{{xyz}} test {{abc}} temp {{var}}";
https://regex101.com/r/r9lmDL/2

Javascript iterate over string looking for multiple character sets

Ok, so I know how to do a standard loop to iterate over a string to find a character or a word that matches a single character or word, but in this instance, I have multiple character sets that I am looking for. Some are letters, some have characters (including protected ones). I can't split it into an array of words on space or anything like that because the character sets might not have a space, so wouldn't split. I suspect I'm going to have to do a regex, but I'm not sure how to set it up. This is basically the pseudo code of what I'm trying to do and I'd appreciate any tips on how to move forward. I apologize if this is an easy thing and I'm missing it, I'm still working on my javascript.
Pseudo code:
var string = "This *^! is abdf random&!# text to x*?ysearch for character sets";
var tempSet = [];
// start a typical for loop
for(string.length bla bla...){
// look for one of those four character sets and if it hits one
if(foundSet == "abdf" | "x*?y" | "*^!" | "&!#")
// push that character set to the tempSet array
tempSet.push(foundSet);
// continue searching for the next set until the string is done
console.log(tempSet);
//expected result = ["*^!", "abdf", "&!#", "x*?y"]
and all the sets are in the array in the order in which they appeared in the string
there is obviously more, but that part I can handle. It's this line
if(??? == "abdf" | "x*?y" | "*^!" | "&!#")
that I don't know really how to tackle. I suspect it should be some kind of regex but can you have a regex like that with a | when doing an if statement? I've done them with a | when doing a map/replace but I've never used a regex in a loop. I also don't know how to get it to search multiple characters at a time. Some of the character sets are 3, some are 4 characters long.
I would appreciate any help or if you have a suggestion on how to approach this in an easier way, that would be great.
Thanks!
You can use a regular expression. Just list all your strings as alternatives separated by |. Characters that have special meaning in regular expressions (e.g. *, ?, ^, $) will need to be escaped with \ (you can safely escape any non-alphanumeric characters -- some will be redundant).
var string = "This *^! is abdf random&!# text to x*?ysearch for character sets";
var tempSet = string.match(/abdf|x\*\?y|\*\^!|&!#/g);
console.log(tempSet);
If you need a loop you can call RegExp.prototype.exec() in a loop.
var string = "This *^! is abdf random&!# text to x*?ysearch for character sets";
var regex = /abdf|x\*\?y|\*\^!|&!#/g;
var tempSet = [];
while (match = regex.exec(string)) {
tempSet.push(match[0]);
}
console.log(tempSet);
A bit more of a manual method than Barmar's excellent RegEx, but it was fun to put together and shows the pieces maybe a bit more clearly:
var text = "This *^! is abdf random&!# text to x*?ysearch for character sets",
detect = ["abdf", "x*?y", "*^!", "&!#"],
haystack = '',
found = [];
text.split('').forEach(function(letter){
haystack += letter;
detect.forEach(function(needle){
if (haystack.indexOf(needle) !== -1
&& found.indexOf(needle) === -1) {
found.push(needle);
}
});
});
console.log(found);
I think what you're looking for is the includes() function.
var sample = "This *^! is abdf random&!# text to x*?ysearch for character
sets";
var toSearch = ["*^!", "abdf", "&!#", "x*?y"];
var tempSet = [];
for (var i = 0; i < toSearch.length; i++) {
if (sample.includes(toSearch[i]){
tempSet.push(toSearch[i]);
}
}
console.log(tempSet);
//expected result = ["*^!", "abdf", "&!#", "x*?y"]
This way you can iterate through an entire array of whatever strings you're searching for and push all matching elements to tempSet.
Note: This is case sensitive, so make sure you consider your check accordingly.
I would just add this as a comment to Kevin's answer if I was able to, but if you need IE support you can also check searchString.indexOf(searchToken) !== -1.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

JavaScript: Replacing characters on both sides of a string

What I want to do is to match characters enclosed by ^^ and replace those ^^ while maintaining the string. In other words, turning this:
^^This is a test^^ this is ^^another test^^
into this:
<sc>This is a test</sc> this is <sc>another test</sc>
I got the regex to match them:
\^{2}[^^]+\^{2}
But I'm stuck there. I'm not sure what to do with the other .replace parameter:
.replace(/\^{2}[^^]+\^{2}/g, WHAT_TO_ADD_HERE?)
Any ideas?
You can use replace with regex and grouping like
var text = '^^This is a test^^ this is ^^another test^^'.replace(/\^\^(.*?)\^\^/g, '<sc>$1</sc>')
Here is a piece of code you can use:
var re = /(\^{2})([^^]+)(\^{2})/g;
var str = '^^This is a test^^ this is ^^another test^^\n\n<sc>This is a test</sc> this is <sc>another test</sc>';
var subst = '<sc>$2</sc>';
var result = str.replace(re, subst);
This is just an enhancement of your regex pattern where I added capturing groups. To improve performance and ensure you will be capturing all symbols between the ^^, you can use only one capturing group and . symbol with non-greedy quantificator:
var re = /\^{2}(.+?)\^{2}/g;
Have a look at the example.
In this case you need to use the group index to wrap the content.
var content = "^^This is a test^^ this is ^^another test^^";
content.replace(/\^{2}(.*?)\^{2}/g, '<sc>$1</sc>');
The (.*?) will help you to group the content and in your replace statement use $1 where 1 is the index of group.

non-capture group still showing in match

I know this topic has been thoroughly covered on StackOverflow, but I can't for the life of me get my regular expression to work. So without further repetitive ado ...
This is what I have.
String: <p model='cat'></p>
Regex: .match(/(?:model=')(.*)(?:')/g)
This is what my expression returns: model='cat'
This is what I want: cat
Why isn't my non capture group ignored? Is it that I don't understand what a non-capturing group does? Why isn't my Regex working?
The entire match will always be group 0, you need to access that specific group (group 1 in this case since the first group is non-capture), you can do it like this:
var str = "<p model='cat'></p>";
var regex = /(?:model=')(.*)(?:')/g
var match = regex.exec(str);
alert(match[1]); // cat
Fiddle
Also, I suppose you are probably wanting several matches within str, you could do that like this:
var str = "<p model='cat'></p><p model='dog'></p><p model='horse'></p>";
var regex = /(?:model=')([^']*)/g
var matches = [];
var match;
while (match = regex.exec(str)) {
matches.push(match[1]);
}
alert(matches); // cat,dog,horse
Fiddle
A non-capturing group is basically just a non-group ― a way to use parentheses without actually treating that part of the pattern as a group.
It looks like what you're actually looking for are the "match prefix but exclude" group (?<=) and the "match suffix but exclude" group (?=).
Note: This type of group does not seem to be supported in Internet Explorer.
If you use these, you get the desired result:
var str = "<p model='cat'></p><p model='dog'></p><p model='horse'></p>";
var regex = /(?<=model=')[^']*(?=')/g
var matches = str.match(regex);
console.log(matches);

Javascript search and replace sequence of characters that contain square brackets

I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want to remove this from the string so I'm using a replace method, code examaple below
var str = 'Nationality [EN] [ESP]';
var find = "[EN]";
var regex = new RegExp(find, "g");
alert(str.replace(regex, ''));
Since [EN] is identified as a character set this will output the string 'Nationality [] [ESP]' but I want to remove the square brackets aswell. I thought that I could escape them using \ but it didn't work
Any advice would be much appreciated
Try setting your regex this way:
var regex = /\[EN\]/g;
If you just want to replace a single instance of it you can just str = str.replace("[EN] ", ""); otherwise; var find = "\\[EN\\]";.

Categories