2f34435-something.jpg
4t44234-something.jpg
5465g67-something.jpg
says I have 3 string above, without using split, how can I do regex to get the value before dash? the length of strings is not consistent though..
One option would be to match one or more non-dash characters at the beginning of the string:
^[^-]+
^ - Anchor that denotes the beginning of the string
[^-] - Character set to match all characters that are not dashes.
+ - One or more occurrences of non-dash characters.
For instance:
'2f34435-something.jpg'.match(/^[^-]+/);
// ["2f34435"]
With the .split() method, you would just need to retrieve the first match:
'2f34435-something.jpg'.split('-')[0];
// "2f34435"
you can use...
/^(.+?)-/gm
which will capture all 3 ( or as many as you have )
You can see it in action here https://regex101.com/r/gD5sU2/2
This will also handle if you get - in the rest of the filename...
such as :-
2f34435-something-else.jpg
4t44234-something.jpg
5465g67-something.jpg
([a-z0-9]+)-[a-z0-9]+.jpg matches each of those strings and the string returned from the first group will match the text before the dash.
Use look ahead for to do it
/^.+?(?=-)/gm
https://regex101.com/r/mW5bO7/2
Related
I'm attempting to get the last word in the following strings.
After about 45 minutes I can't seem to find the right combination of slashes, dashes and brackets.
The closest I've got is
/(?![survey])[a-z]+/gi
It matches the following strings, except for "required" it is returning the match "quired" I'm assuming it's because the re are in the word survey.
survey[1][title]
survey[1][required]
survey[2][anotherString]
You're using a character set, which will exclude any of the characters from being the first character in the match, which isn't what you want. Using plain negative lookahead would be a start:
(?!survey)[a-z]+
But you also want to match the final word, which can be done by matching word characters that are followed with \]$ - that is, by a ] and the end of the string:
[a-z]+(?=\]$)
https://regex101.com/r/rLvsY5/1
If you want to be more efficient, match the whole string, but capture what comes between the square brackets in a capturing group - the last repeated captured group will be in the result:
survey(?:\[(\w+)\])+
https://regex101.com/r/rLvsY5/2
One way to solve this is to match the full line and only capture the part you need.
survey\[\d+\]\[([a-z]+)\]
I am using javascript and looking for a regex which can replace a matched string with the same number of space. For example, I want to match a string which begins with show and ends to the end of line, this is the regex I am using /show .*$/. If users type show dbs then I want to replace with (8 spaces). How can I know the number of characters for the matched string?
I believe the most concise way to achieve such results in javascript with RegEx is to match one part of the string, replace the rest with spaces and concatenate both parts as follows:
str.replace(/^(show)(.*)/, (str, p1, p2) => p1 + p2.replace(/./g, " "));
The first replace will separate the beginning from the end and send those parts as arguments into the method. The first part can be left untouched and the second part transformed into spaces.
you can use .length on the matched element. For example
var pat = /show .*$/
'show dbs'.match(pat)[0].length
will return 8 then you can either concatenate the returned value or use .replace() on it
'show dbs'.replace(/^/, " ".repeat(8)) //we got 8 from the length property
Note: if there are multiple matches you'll have to loop over them
I'm using jQuery. I have to check if a given list of words are in a paragraph or not. I want the exact match of a word or a phrase(whole word match).ie, if i search for 'be' in 'Be a bee', only one match is there. I have done like this.
var searchText="tool,media,be,team";
var regexExactMatch = new RegExp('\^' + searchText.split(",").join("|") + '\$');
if (regexExactMatch.test(item.Name))
{
//Found
}
It is working for one search term, ie, without any comma (eg: media).
But for comma separated search, it will break.
How to do a exact match search for multiple search terms. I'm very very new to regex. Also I have to do the same search for integers and date (MM/dd/yyyy). Thanks in advance.
For full input string match use
new RegExp('^(?:' + searchText.split(",").join("|") + ')$');
^^^ ^
For a whole word search, replace ^ and $ with \b:
new RegExp('\\b(?:' + searchText.split(",").join("|") + ')\\b');
Otherwise, the anchors are applied respectively to the first and last alternatives only (i.e. your regex will look like /^tool|media|be|team$/ looking for tool at the beginning only, media and be anywhere in the string and team only at the end of the string).
Note I am using (?:...) non-capturing group since grouping is only necessary here, not capturing (no storing of the submatch). If you need to access the matched text, you can access the 0th group that equals the whole match.
Also, you do not need those \s before ^ and $, they are not necessary at all and are ignored in the constructor notation since there are no escape sequences like \^ and \$.
Remove the ^ from the beginning and $ from the end of the RegExp. Like this :
var regexExactMatch = new RegExp(searchText.split(",").join("|"));
Reason
^ will set the condition that the matched text need to be at the beginning of the string and $ set the condition that the matched text need to be at the end of the string, which can only happen if there is only that text in the string.
I am trying to clean an input field client side.
Current Value
string = 'word, another word,word,,,,,, another word, ,,;
Desired Value after cleaning
string = 'word,another word,word,another word;
Simplified version of what I have tried http://jsfiddle.net/zg2e7/362/
You can use
var str = 'word,word,word,,,,,new word, , another word';
document.body.innerHTML = str.replace(/(?:\s*,+)+\s*/g, ',');
You need to use g modifier to find and replace all instances
You need to also match optional whitespace between commas and on both sides of them.
Regex explanation:
(?:\s*,+)+ - 1 or more sequences of
\s* - 0 or more whitespace characters
,+ - 1 or more commas.
string = 'word, another word,word,,,,,, another word, ,,';
console.log(string.replace(/(,)[,\s]+|(\s)\s+/g ,'$1').replace(/^,|,$/g,''));
Try using split and trim and map and join rather than regex being that regex can be a bit clunky.
$.map(str.split(','),function(item,i){
if(item.trim()){
return item.trim()
}
}).join(',')
So split the string by the , and then use the map function to combine them. If the item has value after being trimmed then keep the value. Then after it has been mapped to a array of the valid values join them with a comma.
I need to check whether information entered are 3 character long, first one should be 0-9 second A-Z and third 0-9 again.
I have written pattern as below:
var pattern = `'^[A-Z]+[0-9]+[A-Z]$'`;
var valid = str.match(pattern);
I got confused with usage of regex for selecting, matching and replacing.
In this case, does[A-Z] check only one character or whole string ?
Does + separate(split?) out characters?
1) + matches one or more. You want exactly one
2) declare your pattern as a REGEX literal, inside forward slashes
With these two points in mind, your pattern should be
/^[A-Z][0-9][A-Z]$/
Note also you can make the pattern slightly shorter by replacing [0-9] with the \d shortcut (matches any numerical character).
3) Optionally, add the case-insensitive i flag after the final trailing slash if you want to allow either case.
4) If you want to merely test a string matches a pattern, rather than retrieve a match from it, use test(), not match() - it's more efficient.
var valid = pattern.test(str); //true or false
+ means one or more characters so a possible String would be ABCD1234EF or A3B, invalid is 3B or A 6B
This is the regex you need :
^[0-9][A-Z][0-9]$
In this case, does[A-Z] check only one character or whole string ?
It's just check 1 char but a char can be many times in a string..
you should add ^ and $ in order to match the whole string like I did.
Does + separate(split?) out characters?
no.
+ sign just shows that a chars can repeat 1+ times.
"+" means one or more. In your case you should use exact quantity match:
/^\w{1}\d{1}\w{1}$/