Regular Expression in JS - javascript

How do you trim all of the text after a comma using JS?
I have: string = Doyletown, PA
I want: string = Doyletown

var str = 'Doyletown, PA';
var newstr=str.substring(0,str.indexOf(',')) || str;
I added the || str to handle a scenario where the string has no comma

How about a split:
var string = 'Doyletown, PA';
var parts = string.split(',');
if (parts.length > 0) {
var result = parts[0];
alert(result); // alerts Doyletown
}

using regular expression it will be like:
var str = "Doyletown, PA"
var matches = str.match(/^([^,]+)/);
alert(matches[1]);
jsFiddle
btw: I would also prefer .split() method

Or more generally (getting all the words in a comma separated list):
//Gets all the words/sentences in a comma separated list and trims these words/sentences to get rid of outer spaces and other whitespace.
var matches = str.match(/[^,\s]+[^,]*[^,\s]+/g);

Try this:
str = str.replace(/,.*/, '');
Or play with this jsfiddle

Related

Regex remove all string start with special character

I have a string look like:
var str = https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none
I want to remove at start ?pid= to end. The result look like:
var str = https://sharengay.com/movie13.m3u8
I tried to:
str = str.replace(/^(?:?pid=)+/g, "");
But it show error like:
Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat
If you really want to do this at the string level with regex, it's simply replacing /\?pid=.*$/ with "":
str = str.replace(/\?pid=.*$/, "");
That matches ?pid= and everything that follows it (.*) through the end of the string ($).
Live Example:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.replace(/\?pid=.*$/, "");
console.log(str);
You can use split
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"
var result = str.split("?pid=")[0];
console.log(result);
You can simply use split(), which i think is simple and easy.
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.split("?pid");
console.log(str[0]);
You may create a URL object and concatenate the origin and the pathname:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
var url = new URL(str);
console.log(url.origin + url.pathname);
You have to escape the ? and if you want to remove everything from that point you also need a .+:
str = str.replace(/\?pid=.+$/, "")
You can use split function to get only url without query string.
Here is the example.
var str = 'https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none';
var data = str.split("?");
alert(data[0]);

How to replace two characters at the same time with js?

Below is my code.
var str = 'test//123_456';
var new_str = str .replace(/\//g, '').replace(/_/g, '');
console.log(new_str);
It will print test123456 on the screen.
My question is how to do it in same regular express? not replace string twice.
Thanks.
Use character class in the regex to match any character in the collection. Although use repetition (+, 1 or more) for replacing // in a single match.
var new_str = str .replace(/[/_]+/g, '');
var str = 'test//123_456';
var new_str = str.replace(/[/_]+/g, '');
console.log(new_str);
FYI : Inside the character class, there is no need to escape the forward slash(in case of Javascript RegExp).
Use the regex to match the list of character by using regex character class.
var str = "test//123_456";
var nstr = str.replace(/[\/_]/g, '');

Convert URL to string and extract part of to it to a variable [duplicate]

I have this code, it looks alright and is really basic, but i can't make it work:
function checkValid(elem){
var abc = elem.value;
var re = "/[0-9]/";
var match = re.test(abc);
alert(match);
}
It matches 0 and 9, but not 1 to 8, what's wrong here? Thanks.
re is a string, not a RegExp object.
You need to use a regex literal instead of a string literal, like this:
var re = /[0-9]/;
Also, this will return true for any string that contains a number anywhere in the string.
You probably want to change it to
var re = /^[0-9]+$/;
Try removing the double quotes...
var re = /[0-9]/;
Use \d to match a number and make it a regular expresison, not a string:
var abc = elem.value;
var re = /\d/;
var match = re.test(abc);
alert(match);

Removing spaces, hyphen and brackets from a string

I am getting a string like:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
I want to remove the spaces, hyphen and brackets from every number. Something like:
var str = '+911234567891,432123234,12312313456,4325671234';
Please suggest a way to achieve this.
This will do your job:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
var result = str.replace(/[- )(]/g,'');
alert(result);
You can use Regular Expression to replace those items by empty string:
'+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)'.replace(/[\s()-]+/gi, '');
// results in "+911234567891,432123234,12312313456,4325671234"
Hope it helps.

find special text from string in javascript

I have string like #ls/?folder_path=home/videos/
how i can find last text from string? this place is videos
other strings like
#ls/?folder_path=home/videos/
#ls/?folder_path=home/videos/test/testt/
#ls/?folder_path=seff/test/home/videos/
We could use a few more example strings, but based off of your one and only example, here's a rough regex to get you started:
.*?/\?.*?/(.*?)\//
EDIT:
Based on your extended examples:
.*?/\?.*/(.*?)\//
This regex will consume text until the second to last / and capture until the last / in the string.
This will work even if the string doesn't end in /
var str;
var re = /\w+(?=\/?$)/;
str = "#ls/?folder_path=home/videos/"
str.match(re) ; //# => videos
str = "#ls/?folder_path=home/videos/test/testt/"
str.match(re) ; //# => testt
str = "#ls/?folder_path=seff/test/home/videos/"
str.match(re) ; //# => videos
str = "#ls/?folder_path=home/videos/test/testt"
str.match(re) ; //# => testt
\/([^\/]*)\/?$
This regex will match all non / between the last two /. Where the last / is optional. The $ is matching the end of the string.
Your resulting string is then in the first capturing group (because of the ()) $1
You can test it here
There are many ways to do this. One of them:
var str = '#ls/?folder_path=home/videos/'.replace(/\/$/,'');
alert(str.substr(str.lastIndexOf('/')+1)); //=> videos
Alternative without using replace
var str = '#ls/?folder_path=home/videos/'
,str = str.substr(0,str.length-1)
,str = str.substr(str.lastIndexOf('/')+1);
alert(str); //=> videos
If your data is consistent like this string, this is a simple split based way to retreive
your required string: http://jsfiddle.net/EEkLP/
var str="#ls/?folder_path=home/videos/";
var strArr = str.split("/");
alert(strArr[strArr.length-2]);
If it always ends with / then this will works.
var str = '#ls/?folder_path=home/videos/';
var arr = str.split('/');
var index = arr.length-2;
console.log(arr[index]);
If the last word always enclosed with forward slashes, then you can try this -
".+\/([^\/]+)\/$"
or in regex notation
/.+\/([^\/]+)\/$/

Categories