Match only the line which end with specific char - javascript

How to match the line which does not contain the final dot (full stop/period), in order to add it afterwards.
Someword someword someword.
Someword someword someword
Someword someword someword.
These are my unsuccessful attempts:
.+(?=\.)
.+[^.]
--- update
This works for me:
.+\w+(?:\n)
https://regex101.com/r/sR0aD7/1

The following should match a string that ends with anything but dot: [^.]$ - "anything but dot" and end-of-text marker.

How to match the line which does not contain the final dot (full stop/period),
You can use negative lookahead like this:
/(?!\.$)/
OR else you can also inverse test:
if (!/\.$/.test(input)) { console.log("line is not ending with dot"); }

Regular expression is one way i think you can use this method also --->
function lastCharacter(sentence){
var length = sentence.length;
return sentence.charAt(length-1);
}
Example :-
Input ---> Hey JavaScript is damm good.
Use ---> lastCharacter('Hey JavaScript is damm good.');
Output ---> '.'
In other cases you can check with if condition for dot('.').

Just use something like this: [^\.]$
$ - Indicates end of line.
[^...] - Indicates selecting lines not containing "..."
\. - This is the escaped "." Character. It needs to be escaped because . is anything.
Pulling this together, you get a regular expression .+[^\.]$ which will match your line. You will need the m flag (Multiline) for this to work (I believe)

This works for me:
.+\w+(?:\n)
https://regex101.com/r/sR0aD7/1

Related

Remove `-` character at the end of domain name

I have this code:
string.replace(/[~!##$%^&*()_\-+={}[\]|"':;?,/><,\\]/g,'');
I want to remove all invalid characters from domain. It's working fine, but additionally I want to remove - character from the end if it is here.
So, te-!#$##$##st-.com will be te-st.com.
I tried added something like that [-]$, so the code looks like this:
string.replace(/[~!##$%^&`*()_\+={}[\]|"':;?,/><,\\][-]$/g,'')
But this doesn't work, any ideas?
You should use an alternation here:
string.replace(/[~!##$%^&`*()_\+={}[\]|"':;?,\/><,\\]|-+(?=\.)/g, '')
Demo
This regex pattern says to match:
[~!##$%^&*()_\+={}[\]|"':;?,\/><,\\] match a symbol
| OR
-+(?=\.) match 1 or more dashes which are followed by dot (but do not consume the dot)
Maybe use replacer function:
const firstIndex = string.indexOf('-');
string.replace(/[~!##$%^&*()_\-+={}[\]|"':;?,/><,\\]/g,
(match,offset) => offset === firstIndex ? match : ''
);

javascript regex insert new element into expression

I am passing a URL to a block of code in which I need to insert a new element into the regex. Pretty sure the regex is valid and the code seems right but no matter what I can't seem to execute the match for regex!
//** Incoming url's
//** url e.g. api/223344
//** api/11aa/page/2017
//** Need to match to the following
//** dir/api/12ab/page/1999
//** Hence the need to add dir at the front
var url = req.url;
//** pass in: /^\/api\/([a-zA-Z0-9-_~ %]+)(?:\/page\/([a-zA-Z0-9-_~ %]+))?$/
var re = myregex.toString();
//** Insert dir into regex: /^dir\/api\/([a-zA-Z0-9-_~ %]+)(?:\/page\/([a-zA-Z0-9-_~ %]+))?$/
var regVar = re.substr(0, 2) + 'dir' + re.substr(2);
var matchedData = url.match(regVar);
matchedData === null ? console.log('NO') : console.log('Yay');
I hope I am just missing the obvious but can anyone see why I can't match and always returns NO?
Thanks
Let's break down your regex
^\/api\/ this matches the beginning of a string, and it looks to match exactly the string "/api"
([a-zA-Z0-9-_~ %]+) this is a capturing group: this one specifically will capture anything inside those brackets, with the + indicating to capture 1 or more, so for example, this section will match abAB25-_ %
(?:\/page\/([a-zA-Z0-9-_~ %]+)) this groups multiple tokens together as well, but does not create a capturing group like above (the ?: makes it non-captuing). You are first matching a string exactly like "/page/" followed by a group exactly like mentioned in the paragraph above (that matches a-z, A-Z, 0-9, etc.
?$ is at the end, and the ? means capture 0 or more of the precending group, and the $ matches the end of the string
This regex will match this string, for example: /api/abAB25-_ %/page/abAB25-_ %
You may be able to take advantage of capturing groups, however, and use something like this instead to get similar results: ^\/api\/([a-zA-Z0-9-_~ %]+)\/page\/\1?$. Here, we are using \1 to reference that first capturing group and match exactly the same tokens it is matching. EDIT: actually, this probably won't work, since the text after /api/ and the text after /page/ will most likely be different, carrying on...
Afterwards, you are are adding "dir" to the beginning of your search, so you can now match someting like this: dir/api/abAB25-_ %/page/abAB25-_ %
You have also now converted the regex to a string, so like Crayon Violent pointed out in their comment, this will break your expected funtionality. You can fix this by using .source on your regex: var matchedData = url.match(regVar.source); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source
Now you can properly match a string like this: dir/api/11aa/page/2017 see this example: https://repl.it/Mj8h
As mentioned by Crayon Violent in the comments, it seems you're passing a String rather than a regular expression in the .match() function. maybe try the following:
url.match(new RegExp(regVar, "i"));
to convert the string to a regular expression. The "i" is for ignore case; don't know that's what you want. Learn more here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Regex to match only when certain characters follow a string

I need to find a string that contains "script" with as many characters before or after, and enclosed in < and >. I can do this with:<*script.*>
I also want to match only when that string is NOT followed by a <
The closest I've come, so far, is with this: (<*script.*>)([^=?<*]*)$
However, that will fail for something like <script></script> because the last > isn't followed by a < (so it doesn't match).
How can I check if only the the first > is followed by < or not?
For example,
<script> abc () ; </script> MATCH
<< ScriPT >abc (”XXX”);//<</ ScriPT > MATCH
<script></script> DON'T MATCH
And, a case that I still am working on:
<script/script> DON'T MATCH
Thanks!
You were close with your Regex. You just needed to make your first query non-greedy using a ? after the second *. Try this out:
(?i)<*\s*script.*?>[^<]+<*[^>]+>
There is an app called Expresso that really helps with designing Regex strings. Give it a shot.
Explanation: Without the ? non-greedy argument, your second * before the first > makes the search go all the way to the end of the string and grab the > at the end right at that point. None of the other stuff in your query was even being looked at.
EDIT: Added (?i) at the beginning for case-insensitivity. If you want a javascript specific case-insensitive regex, you would do that like this:
/<*\s*script.*?>[^<]+<*[^>]+>/i
I noticed you have parenthesis in your regex to make groups but you didn't specifically say you were trying to capture groups. Do you want to capture what's between the <script> and </script>? If so, that would be:
/<*\s*script.*?>([^<]+)<*[^>]+>/i
If I understand what you are looking for give this a try:
regex = "<\s*script\s*>([^<]+)<"
Here is an example in Python:
import re
textlist = ["<script>show this</script>","<script></script>"]
regex = "<\s*script\s*>([^<]+)"
for text in textlist:
thematch = re.search(regex, text, re.IGNORECASE)
if thematch:
print ("match found:")
print (thematch.group(1))
else:
print ("no match sir!")
Explanation:
start with < then possible spaces, the word script, possible spaces, a >
then capture all (at least 1) non < and make sure that's followed by a <
Hope that helps!
This would be better solved by using substring() and/or indexOf()
JavaScript methods

Using jQuery to find a substring

Say you have a string: "The ABC cow jumped over XYZ the moon" and you want to use jQuery to get the substring between the "ABC" and "XYZ", how would you do this? The substring should be "cow jumped over". Many thanks!
This has nothing to do with jQuery, which is primarily for DOM traversal and manipulation. You want a simple regular expression:
var str = "The ABC cow jumped over XYZ the moon";
var sub = str.replace(/^.*ABC(.*)XYZ.*$/m, '$1');
The idea is you're using a String.replace with a regular expression which matches your opening and closing delimiters, and replacing the whole string with the part matched between the delimiters.
The first argument is a regular expression. The trailing m causes it to match over multiple lines, meaning your text between ABC and XYZ may contain newlines. The rest breaks down as follows:
^ start at the beginning of the string
.* a series of 0 or more characters
ABC your opening delimiter
(.*) match a series of 0 or more characters
XYZ your closing delimiter
.* a series of 0 or more characters
$ match to the end of the string
The second parameter, the replacement string, is '$1'. replace will substitute in parenthesized submatchs from your regular exprsesion - the (.*) portion from above. Thus the return value is the entire string replace with the parts between the delimiters.
You may not need to use jQuery on this one. I'd do something like this:
function between(str, left, right) {
if( !str || !left || !right ) return null;
var left_loc = str.indexOf(left);
var right_loc = str.indexOf(right);
if( left_loc == -1 || right_loc == -1 ) return null;
return str.substring(left_loc + left.length, right_loc);
}
No guarantees the above code is bug-free, but the idea is to use the standard substring() function. In my experience these types of functions work the same across all browsers.
Meagar, your explanation is great, and clearly explains who it works.
Just a few minor questions:
Are the () parenthesis required ONLY as a way to indicate a submatch in the second parameter of the relpace function or would this also identify the submatches: /^.*ABC.XYZ.$/ but not work for what we are trying to do in this case?
Does this regular expression have 7 submatches:
^
.*
ABC
.*
XYZ
.*
$
Does the $1 mean to use the first parenthesized submatch? At first I thought it might mean to use the second submatch in the series (the first being $0).
Thanks,
Steve
Just to show you how you would use jQuery and meagar's regex. Let's say that you've got an HTML page with the following P tag:
<p id="grabthis">The ABC cow jumped over XYZ the moon</p>
To grab the string, you would use the following jQuery/JavaScript mix (sounds kind of stupid, since jQuery is JavaScript, but see jQuery as a JavaScript DOM library):
$(document).ready(function() { // Wait until the document has been fully loaded
var pStr=$("#grabthis").text(); // Grab the text from the P tag and put it into a JS variable
var subStr=pStr.replace(/^.*ABC(.*)XYZ.*$/m, '$1'); // Run the regex to grab the middle string
alert(subStr); // Output the grabbed middle string
});
Or the shorter version:
$(document).ready(function() {
alert($("#grabthis").text().replace(/^.*ABC(.*)XYZ.*$/m, '$1'));
});
The replace function is a JavaScript function. I hope this clears the confusion.

Regex to get string between curly braces

Unfortunately, despite having tried to learn regex at least one time a year for as many years as I can remember, I always forget as I use them so infrequently. This year my new year's resolution is to not try and learn regex again - So this year to save me from tears I'll give it to Stack Overflow. (Last Christmas remix).
I want to pass in a string in this format {getThis}, and be returned the string getThis. Could anyone be of assistance in helping to stick to my new year's resolution?
Related questions on Stack Overflow:
How can one turn regular quotes (i.e. ', ") into LaTeX/TeX quotes (i.e. `', ``'')
Regex: To pull out a sub-string between two tags in a string
Regex to replace all \n in a String, but no those inside [code] [/code] tag
Try
/{(.*?)}/
That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy). The parentheses let you extract the matched portion.
Another way would be
/{([^}]*)}/
This matches any character except a } char (another way of not being greedy)
/\{([^}]+)\}/
/ - delimiter
\{ - opening literal brace escaped because it is a special character used for quantifiers eg {2,3}
( - start capturing
[^}] - character class consisting of
^ - not
} - a closing brace (no escaping necessary because special characters in a character class are different)
+ - one or more of the character class
) - end capturing
\} - the closing literal brace
/ - delimiter
If your string will always be of that format, a regex is overkill:
>>> var g='{getThis}';
>>> g.substring(1,g.length-1)
"getThis"
substring(1 means to start one character in (just past the first {) and ,g.length-1) means to take characters until (but not including) the character at the string length minus one. This works because the position is zero-based, i.e. g.length-1 is the last position.
For readers other than the original poster: If it has to be a regex, use /{([^}]*)}/ if you want to allow empty strings, or /{([^}]+)}/ if you want to only match when there is at least one character between the curly braces. Breakdown:
/: start the regex pattern
{: a literal curly brace
(: start capturing
[: start defining a class of characters to capture
^}: "anything other than }"
]: OK, that's our whole class definition
*: any number of characters matching that class we just defined
): done capturing
}: a literal curly brace must immediately follow what we captured
/: end the regex pattern
Try this:
/[^{\}]+(?=})/g
For example
Welcome to RegExr v2.1 by #{gskinner.com}, #{ssd.sd} hosted by Media Temple!
will return gskinner.com, ssd.sd.
Try this
let path = "/{id}/{name}/{age}";
const paramsPattern = /[^{}]+(?=})/g;
let extractParams = path.match(paramsPattern);
console.log("extractParams", extractParams) // prints all the names between {} = ["id", "name", "age"]
Here's a simple solution using javascript replace
var st = '{getThis}';
st = st.replace(/\{|\}/gi,''); // "getThis"
As the accepted answer above points out the original problem is easily solved with substring, but using replace can solve the more complicated use cases
If you have a string like "randomstring999[fieldname]"
You use a slightly different pattern to get fieldname
var nameAttr = "randomstring999[fieldname]";
var justName = nameAttr.replace(/.*\[|\]/gi,''); // "fieldname"
This one works in Textmate and it matches everything in a CSS file between the curly brackets.
\{(\s*?.*?)*?\}
selector {.
.
matches here
including white space.
.
.}
If you want to further be able to return the content, then wrap it all in one more set of parentheses like so:
\{((\s*?.*?)*?)\}
and you can access the contents via $1.
This also works for functions, but I haven't tested it with nested curly brackets.
You want to use regex lookahead and lookbehind. This will give you only what is inside the curly braces:
(?<=\{)(.*?)(?=\})
i have looked into the other answers, and a vital logic seems to be missing from them . ie, select everything between two CONSECUTIVE brackets,but NOT the brackets
so, here is my answer
\{([^{}]+)\}
Regex for getting arrays of string with curly braces enclosed occurs in string, rather than just finding first occurrence.
/\{([^}]+)\}/gm
var re = /{(.*)}/;
var m = "{helloworld}".match(re);
if (m != null)
console.log(m[0].replace(re, '$1'));
The simpler .replace(/.*{(.*)}.*/, '$1') unfortunately returns the entire string if the regex does not match. The above code snippet can more easily detect a match.
Try this one, according to http://www.regextester.com it works for js normaly.
([^{]*?)(?=\})
This one matches everything even if it finds multiple closing curly braces in the middle:
\{([\s\S]*)\}
Example:
{
"foo": {
"bar": 1,
"baz": 1,
}
}
You can use this regex recursion to match everythin between, even another {} (like a JSON text) :
\{([^()]|())*\}
Even this helps me while trying to solve someone's problem,
Split the contents inside curly braces ({}) having a pattern like,
{'day': 1, 'count': 100}.
For example:
#include <iostream>
#include <regex>
#include<string>
using namespace std;
int main()
{
//string to be searched
string s = "{'day': 1, 'count': 100}, {'day': 2, 'count': 100}";
// regex expression for pattern to be searched
regex e ("\\{[a-z':, 0-9]+\\}");
regex_token_iterator<string::iterator> rend;
regex_token_iterator<string::iterator> a ( s.begin(), s.end(), e );
while (a!=rend) cout << " [" << *a++ << "]";
cout << endl;
return 0;
}
Output:
[{'day': 1, 'count': 100}] [{'day': 2, 'count': 100}]
Your can use String.slice() method.
let str = "{something}";
str = str.slice(1,-1) // something

Categories

Resources