This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 7 years ago.
I can get background image by doing
$('#item').css('background-image').
but the result is url('www.example.com/something/123.png')
How can get get only the 123 value? I can't use string position because the value of the img name doesn't' necessary is 3 characters.
You can use lastIndexOf and substring:
var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex + 1).replace(".png","");
or using .split() and .pop()
var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");
Related
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Regular expression to match string starting with a specific word
(10 answers)
Regular expression - starting and ending with a character string
(3 answers)
Closed 3 years ago.
I am trying to pull out all the URLs from a text entered by the user by doing the following but am not able to get the desired result.
let regexp = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]+$/igm;
let str = "https://mysleepyhead.com http://skreem.io";
let array = [...str.matchAll(regexp)];
console.log(array);
Desired output would be
Array ['https://mysleepyhead.com', 'http://skreem.io']
This question already has answers here:
Javascript nth occurance of a string and extract the sub string upto that
(3 answers)
Regex expression to cut string at nth occurrence of character and return first part of string
(3 answers)
Cutting a string at nth occurrence of a character
(5 answers)
Closed 3 years ago.
I want to get a substring that is before the nth point.
For example I have:
let str = "my.string.is.like.that"
Now suppose I want substr= "my.string.is.like" that is all before the 4th point. How can I do that?
You can use split and join,
Second parameter in split is used to limit the number of element to be included in final output
let str = "my.string.is.like.that"
let limited = str.split('.',4).join('.')
console.log(limited)
This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?
You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
Closed 4 years ago.
I'm trying to return how many times the string s.t() was found in a string, but I can't get the correct regex for this...
For example
var string = 'function(test) {s.t(); s.t(dsabf); s.t();}'
var re = new RegExp('s\.t\(\)', "g");
return re;
should return an array of 2 elements ['s.t()', 's.t()'] but instead it has 3 elements ['s.t', 's.t', 's.t']
I've also tried with ^s\t\(\)$ but this returns no match...
How can I fix my regex in order to make this work as expected?