I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");
Related
Lets say I am receiving a string like so:
var string = "example_string"
var otherString = "example_string_two"
And I want to manipulate it to output like this:
string = "exampleString"
otherString = "ExampleStringTwo"
Basically, I want to find any underscore characters in a string and remove them. If there is a letter after the underscore, then it should be capitalized.
Is there a fast way to do this in regex?
You could look for the start of the string or underscore and replace the found part with an uppercase character.
var string= 'example_string_two';
console.log(string.replace(/(^|_)./g, s => s.slice(-1).toUpperCase()));
A regular expression like /_([a-zA-Z])/g will do with a proper callback function in String.prototype.replace. See snippet below.
function camelize (dasherizedStr) {
return dasherizedStr
.replace(/_([a-zA-Z])/g, function (m1, m2) {
return m2.toUpperCase()
});
}
console.log('example_string_foo:', camelize('example_string_foo'));
console.log('foo_Bar:', camelize('foo_Bar'));
Yeah you could use regex methods and simply replace the underscore and i'll give you an example :
var string = "example_string"
string.replace('_','');
But you could also do this in classic JS, which is pretty fast in on it's self
Example:
var string = "example_string"
string.split('_').join('');
If you are looking for something more, please comment below.
You can easily replace using JavaScript
var string= 'example_string_two';
console.log(string.replaceAll('_', ' '))
Output : 'example string two'
you can replace any word, underscore, dashes using javascript
here is code
var str= 'example_string_two';
console.log(var newStr = str.replace("_", " "));
output:
examplestringtwo
All I need to do here is to add a variable before each specific string.
Example:
var exampleString = "blabla:test abcde 123test:123";
var formattedString = "el.blabla:test abcde el.123test:123";
As you can see, when I have something like "XXX:XXX", I need to add a variable before it.
I have the Regex to find "XXX:"
var regex = new RegExp(/\w+([aA-zZ]:)/g)
But when I try to replace it, it replaces all instead of adding the variable "el."
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(new RegExp(/\w+([aA-zZ]:)/g), 'el.');
// formattedString is now "el.test abcde el.123"
// Instead of "el.blabla:test abcde el.123test:123"
Could anyone makes this work ? Thanks :)
Source: Javascript Regex: How to put a variable inside a regular expression?
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(/\w*:\w*/gi, 'el.$&');
console.log(formattedString);
Regex use and Explanation Here https://regex101.com/r/U2KeXi/3
Sample Fiddle here https://jsfiddle.net/a8wyLb0g/2/
You need to use ^ to match only at the beginning. And remove the g modifier, since you only want to replace once, not every time.
There's also no reason to use new RegExp(), just use a RegExp literal.
In the replacement string, you need to use $& to copy the original string into the replacement.
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(/^\w+[a-z]:/i, 'el.$&');
console.log(formattedString);
Also, the proper way to match all letters in either case is with [A-Za-z], not [aA-zZ], or use the i modifier to make the regexp case-insensitive. Your regexp matches all characters in the range A-z, which includes lots of punctuation characters that are between the uppercase letters and lowercase letters in the ASCII code.
Just use this
exampleString.replace(/(\w*):(\w*)/gi, 'el.$1:$2');
REGEXP explanation :
capturing group (\w*) is for capturing any alphabets in any number of occurance,
$1 and $2 specifies the first and second capturing group.
You should use a function like insertAt instead replace, see following example:
String.prototype.insertAt=function(index, string) {
return this.substr(0, index) + string + this.substr(index);
}
var exampleString = "blabla:test abcde 123test:123";
var regex = new RegExp(/\w+([aA-zZ]:)/g)
var formattedString = exampleString;
while ( (result = regex.exec(exampleString)) ) {
formattedString = formattedString.insertAt(result.index, "el.");
}
console.log(formattedString);
I hope it helps you, bye.
Let's say I have a string that starts by 7878 and ends by 0d0a or 0D0A such as:
var string = "78780d0101234567890123450016efe20d0a";
var string2 = "78780d0101234567890123450016efe20d0a78780d0103588990504943870016efe20d0a";
var string 3 = "78780d0101234567890123450016efe20d0a78780d0103588990504943870016efe20d0a78780d0101234567890123450016efe20d0a"
How can I split it by regex so it becomes an array like:
['78780d0101234567890123450016efe20d0a']
['78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a']
['78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a']
You can split the string with a positive lookahead (?=7878). The regex isn't consuming any characters, so 7878 will be part of the string.
var rgx = /(?=7878)/;
console.log(string1.split(rgx));
console.log(string2.split(rgx));
console.log(string3.split(rgx));
Another option is to split on '7878' and then take all the elements except first and add '7878' to each of them. For example:
var arr = string3.split('7878').slice(1).map(function(str){
return '7878' + str;
});
That works BUT it also matches strings that do NOT end on 0d0a. How
can I only matches those ending on 0d0a OR 0D0A?
Well, then you can use String.match with a plain regex.
console.log(string3.match(/7878.*?0d0a/ig));
I have this string
'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}'
I want to replace 2014-07-31 with 2014-01-01, i.e. the substring contained between '"date_from":"' and '","', using a regular expression in javascript. I have written this code but it doesn't work:
var qs = 'bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}};'
var regEx = /^(.*?date_from":")[^"]*(".*)$/;
qs = qs.replace(regEx, '2014-01-01');`
You don't need a regex to do that:
eval('bookmarkState={"params":{"date_from":"2014-07-31","date_to":"2014-10-01"}}');
bookmarkState.params.date_from = '1988-04-12';
console.log(JSON.stringify(bookmarkState));
^(.*?date_from":")[^"]*(",".*)$
Try this.Replace by $1<your string>$2.See demo.
http://regex101.com/r/qZ0uP0/1
I'm sure this is an easy one, but I can't find it on the net.
This code:
var new_html = "foo and bar(arg)";
var bad_string = "bar(arg)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
sets bad_start to -1 (not found). If I remove the (arg), it runs as expected (bad_start == 8). Is there something I can do to make the (very handy) "new Regexp" syntax work, or do I have to find another way? This example is trivial, but in the real app it would be doing global search and replace, so I need the regex and the "g". Or do I?
TIA
Escape the brackets by double back slashes \\. Try this.
var new_html = "foo and bar(arg)";
var bad_string = "bar\\(arg\\)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
Demo
Your RegEx definition string should be:
var bad_string = "bar\\(arg\\)";
Special characters need to be escaped when using RegEx, and because you are building the RegEx in a string you need to escape your escape character :P
http://www.regular-expressions.info/characters.html
You need to escape the special characters contained in string you are creating your Regex from. For example, define this function:
function escapeRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}
And use it to assign the result to your bad_string variable:
let bad_string = "bar(arg)"
bad_string = escapeRegex(bad_string)
// You can now use the string to create the Regex :v: