I know the regex that separates two words as following:
input:
'WonderWorld'
output:
'Wonder World'
"WonderWorld".replace(/([A-Z])/g, ' $1');
Now I am looking to remove number in year format from string, what changes should be done in the above code to get:
input
'WonderWorld 2016'
output
'Wonder World'
You can match the location before an uppercase letter (but excluding the beginning of a line) with \B(?=[A-Z]) and match the trailing spaces if any with 4 digits right before the end (\s*\b\d{4}\b). In a callback, check if the match is not empty, and replace accordingly. If a match is empty, we matched the location before an uppercase letter (=> replace with a space) and if not, we matched the year at the end (=> replace with empty string). The four digit chunks are only matched as whole words due to the \b word boundaries around the \d{4}.
var re = /\B(?=[A-Z])|\s*\d{4}\b/g;
var str = 'WonderWorld 2016';
var result = str.replace(re, function(match) {
return match ? "" : " ";
});
document.body.innerHTML = "<pre>'" + result + "'</pre>";
A similar approach, just a different pattern for matching glued words (might turn out more reliable):
var re = /([a-z])(?=[A-Z])|\s*\b\d{4}\b/g;
var str = 'WonderWorld 2016';
var result = str.replace(re, function(match, group1) {
return group1 ? group1 + " " : "";
});
document.body.innerHTML = "<pre>'" + result + "'</pre>";
Here, ([a-z])(?=[A-Z]) matches and captures into Group 1 a lowercase letter that is followed with an uppercase one, and inside the callback, we check if Group 1 matched (with group1 ?). If it matched, we return the group1 + a space. If not, we matched the year at the end, and remove it.
Try this:
"WonderWorld 2016".replace(/([A-Z])|\b[0-9]{4}\b/g, ' $1')
How about this, a single regex to do what you want:
"WonderWorld 2016".replace(/([A-Z][a-z]+)([A-Z].*)\s.*/g, '$1 $2');
"Wonder World"
get everything apart from digits and spaces.
re-code of #Wiktor Stribiżew's solution:
str can be any "WonderWorld 2016" | "OneTwo 1000 ThreeFour" | "Ruby 1999 IamOnline"
str.replace(/([a-z])(?=[A-Z])|\s*\d{4}\b/g, function(m, g) {
return g ? g + " " : "";
});
import re
remove_year_regex = re.compile(r"[0-9]{4}")
Test regex expression here
Related
function dashes(str) {
str = str.replace(/_/g,' ').replace(/\s+/g,"-").toLowerCase();
return str;
}
//test cases
dashes("thisCakeIsDelicious");
dashes("TheBig cat was Boastful");
the desired output respectively are: "this-cake-is-delicious" and "the-big-cat-was-boastful".
How do i put a space between "TheBig" without contradicting the space before "Boastful". I have tried regex particular capital letters but as you can see Big and Boastful start with B.
This should work, but I'm not absolutely sure about the requirements so I decided to divide by word not by letter (so LLLLie will result in llllie, not l-l-l-lie)
([a-z]+)([A-Z]{1})|(\s)
Matches:
([a-z]+): 1 or more lowercase letter
([A-Z]{1}): 1 uppercase letter
(\s+): one or more whitespace character (equal to [\r\n\t\f\v ])
var dasher = function(str) {
return str
.trim()
.replace(/([a-z]+)([A-Z]{1})|(\s+)/g, '$1-$2')
.toLowerCase();
}
console.log(dasher('thisCakeIsDelicious'));
console.log(dasher('TheBig cat was Boastful'));
console.log(dasher('The cakeIsA LLLLLie'));
console.log(dasher(' JeremySpoke inClass Today'));
x = "thisCakeIsDelicious";
x.replace(/([a-z](?=[A-Z]))| /g, '$1-');
results in
this-Cake-Is-Delicious,
and
x = "TheBig cat was Boastful";
x.replace(/([a-z](?=[A-Z]))| /g, '$1-');
results in
The-Big-cat-was-Boastful
You could use a callback in the replace function
function dashes(str) {
return str.replace(/(?!^)(\s?[A-Z\s+])/g, function(x) {
return '-' + x.trim();
}).toLowerCase();
}
//test cases
console.log( dashes("thisCakeIsDelicious") );
console.log( dashes("TheBig cat was Boastful") );
How to capitalize each words on starting of a string and after dot(.) sign?
I made a research on google and stackoverflow, below are the codes that I achieved but this will only capitalize starting of a string. Example as belows;
var str = 'this is a text. hello world!';
str = str.replace(/^(.)/g, str[0].toUpperCase());
document.write(str);
I want the string to be This is a text. Hello world!.
I have tried to use css, text-transform: capitalize; but this will result in each word to be capitalize.
I use a function like this, that takes an optional second parameter that will convert the entire string to lowercase initially. The reason is that sometimes you have a series of Title Case Items. That You Wish to turn into a series of Title case items. That you wish to have as sentence case.
function sentenceCase(input, lowercaseBefore) {
input = ( input === undefined || input === null ) ? '' : input;
if (lowercaseBefore) { input = input.toLowerCase(); }
return input.toString().replace( /(^|\. *)([a-z])/g, function(match, separator, char) {
return separator + char.toUpperCase();
});
}
The regex works as follows
1st Capturing Group (^|\. *)
1st Alternative ^
^ asserts position at start of the string
2nd Alternative \. *
\. matches the character `.` literally (case sensitive)
* matches the character ` ` literally (case sensitive)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group ([a-z])
Match a single character present in the list below [a-z]
a-z a single character in the range between a (ASCII 97) and z (ASCII 122) (case sensitive)
You would implement it in your example like so:
var str = 'this is a text. hello world!';
str = sentenceCase(str);
document.write(str); // This is a text. Hello world!
Example jsfiddle
PS. in future, i find regex101 a hugely helpful tool for understanding and testing regex's
If you are using jquery, use this code
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
var str = "my name is Jhon. are you good. is it";
var str1 = str.split('.');
var str2 = "";
$.each(str1,function(i){
str2 += capitalize($.trim(str1[i]))+'. ';
});
console.log(str2);
catch the out put in str2.
in my case its like the following.
My name is Jhon. Are you good. Is it.
My goal is :
Delete all.
except the numbers , but delete the zeros who before numbers 1 to 9
And I have this regex:
var validValue = inputValue.replace(/[^\d]/g, '').replace(/^0*/g, '');
But I want to make it in a one replace()
So how can I do that ?
You want to remove all leading zeros and all non-digit symbols. It can be done with
/^0+|\D+/g
See the regex demo
The regex matches
^0+ - 1 or more leading digits (those at the beginning of the string)
| - or
\D+ - one or more non-digit symbols
var re = /^0*|\D+/g;
var str = '00567600ffg5566';
var result = str.replace(re, '');
document.body.innerHTML = str + " >>> " + result;
Let's say there is a textarea with the following value ('*' being used as a bullet point):
*south
*north
*west
I want to be able to automatically generate an array of these words using Regular Expression, like this.
["south","north","west"]
Below is the expression I tried.
/\*.*/gm.exec(text)
Unfortunately it returns this instead.
["*south"]
Apparently, RegExp recognizes there is a line break such that it only returns the first item, yet it doesn't pick up the 2nd and 3rd lines.
/\*.*/gm.exec('*south \n *north')
This also has the same result.
You need to tell the regex engine to match at the beginning of a line with ^, and capture the part after the first * with a pair of unescaped parentheses. Then, you can use RegExp#exec() in a loop, and get the value you need in Group 1. The ^\s*\*\s*(.*) regex matches:
^ - start of a line (due to /m multiline modifier)
\s* - zero or more whitespace symbols
\* - a literal asterisk
\s* - again, optional whitespace(s)
(.*) - zero or more characters other than a newline.
var re = /^\s*\*\s*(.*)/gm;
var str = '*south\n *north\n* west ';
var res = [];
while ((m = re.exec(str)) !== null) {
res.push(m[1]);
}
document.write("<pre>" + JSON.stringify(res, 0, 4) + "</pre>");
Another solution:
Split with newline (a regex is possible here if there can be \r or \n) and then get rid of the initial *:
var str = '*south\n*north\n*west ';
var res = [];
str.split(/[\r\n]+/).forEach(function(e) {
res.push(e.replace(/^\s*\*\s*/, ''));
});
document.write("<pre>" + JSON.stringify(res, 0, 4) + "</pre>");
#VKS solution works, but if it is not mandatory to use regex then try this fiddle
<textarea id="textA1"></textarea>
$( "#textA1" ).blur( function(){
var value = $( this ).val();
console.log( value.split( "\n" ) );
} )
You will have to run a loop.
var re = /\*(.*)/gm;
var str = '*south\n*north\n*west ';
var m;
while ((m = re.exec(str)) !== null) {
// View your result using the m-variable.
// eg m[0] etc.
}
See demo.
https://regex101.com/r/iJ7bT6/11
or you an split by (?=\*).See demo.
https://regex101.com/r/iJ7bT6/12
I have the following URL structure:
https://api.bestschool.com/student/1102003120009/tests/json
I want to cut the student ID from the URL. So far I've came up with this:
/(student\/.*[^\/]*)/
which returns
student/1102003120009/tests/json
I only want the ID.
Your regex (student\/.*[^\/]*) matches and captures into Group 1 a literal sequence student/, then matches any characters other than a newline, 0 or more occurrences (.*) - that can match the whole line at once! - and then 0 or more characters other than /. It does not work because of .*. Also, a capturing group should be moved to the [^\/]* pattern.
You can use the following regex and grab Group 1 value:
student\/([^\/]*)
See regex demo
The regex matches student/ literally, and then matches and captures into Group 1 zero or more symbols other than /.
Alternatively, if you want to avoid using capturing, and assuming that the ID is always numeric and is followed by /tests/, you can use the following regex:
\d+(?=\/tests\/)
The \d+ matches 1 or more digits, and (?=\/tests\/) checks if right after the digits there is a /tests/ character sequence.
var re = /student\/([^\/]*)/;
var str = 'https://api.bestschool.com/student/1102003120009/tests/json';
var m = str.match(re);
if (m !== null) {
document.getElementById("r").innerHTML = "First method : " + m[1] + "<br/>";
}
var m2 = str.match(/\d+(?=\/tests\/)/);
if (m2 !== null) {
document.getElementById("r").innerHTML += "Second method: " + m2;
}
<div id="r"/>