I have the following variable:
var test = category~[330526|330519]^Size{1}~[m]
How do I match just to get category~[330526|330519] using regex.
This value can also change so it could be category~[3303226|333219]
Just try with:
test.split('^')[0];
var test = 'category~[330526|330519]^Size{1}~[m]';
var result = test.split('^').shift();
FIDDLE
You could;
result = test.substr(0, test.indexOf("]") +1);
This should do it:
category~\[\d+\|\d+\]
If you insist on using a Regex, this one doesn't care about what the category is (.*~\[\d+\|\d+\]). Here is a Rubular to prove it. But I have to say, the answer by #hsz is really the most insightful. The split is probably the right tool for the job.
Yet another approach...
var test = 'category~[330526|330519]^Size{1}~[m]';
var result = test.replace(/\^.+/,"");
"category~[330526|330519]^Size{1}~[m]".replace(/(category~[\d+\|\d+]).*/,"$1"), you should get the string, or you can use match as well.
Related
I scrape sites for a database with a chrome extension,
need assitance with a JavaScript Clean up function
e.g
https://www.alibaba.com/product-detail/_60789694386.html?spm=a2700.galleryofferlist.normalList.1.5be41470uWBNGm&s=p
my target output is:
_60789694386.html
everything past .html needs to be removed, but since it is diffrent in each URL - i'm lost
the output is in a .csv file, in which i run a JavaScript to clean up the data.
this.values[8] = this.values[8].replace("https://www.alibaba.com/product-detail/","");
this.values[8] is how i target the column in the script. (Column 8 holds the URL)
Well, you can use split.
var final = this.values[8].split('.html')[0]
split gives you an array of items split by a string, in your case'.html', then you take the first one.
Consider using substr
this.values[8] = this.values[8].substr(0,this.values[8].indexOf('?'))
You can use split method to divide text from ? as in example.
var link = "https://www.alibaba.com/product-detail/_60789694386.html?spm=a2700.galleryofferlist.normalList.1.5be41470uWBNGm&s=p"
var result = link.split('?')[0].replace("https://www.alibaba.com/product-detail/","");
console.log(result);
Not sure i understood your problem, but try this
var s = 'https://www.alibaba.com/product-detail/_60789694386.html?spm=a2700.galleryofferlist.normalList.1.5be41470uWBNGm&s=p'
s = s.substring(0, s.indexOf('?'));
console.log( s );
For when you don't care about readability...
this.values[8] = new URL(this.values[8]).pathname.split("/").pop().replace(".html","");
Alternate, without using split
var link = "https://www.alibaba.com/product-detail/_60789694386.html?spm=a2700.galleryofferlist.normalList.1.5be41470uWBNGm&s=p"
var result = link.replace('https://www.alibaba.com/product-detail/', '').replace(/\?.*$/, '');
console.log(result);
You can use the regex to get it done. As of my knowledge you do something like:
var v = "https://www.alibaba.com/product-detail/_60789694386.html?spm=a2700.galleryofferlist.normalList.1.5be41470uWBNGm&s=p"
result = (v.match(/[^\/]+$/)[0]);
result = result.substring(0,result.indexOf('?'));
console.log(result); // will return _60789694386.html
Desired functionality:
var myString = "123a4 1b234";
console.log(myString.allInstancesOfLetter().something();
//"123A4 1B234"
Is there a way to do this without having to complicate things with an array of all 26 letters etc.?
edit: toUpperCase() causing confusion, I didn't really think of that interaction, I was just choosing a random function.
You can set a callback function as second parameter when using the replace method that will be applied to each ocurrence in a set RegExp.
const myString = "123a4 1b234";
const result = myString.replace(/[a-z]/g, function(char) {
return char.toUpperCase();
});
console.log(result);
toUpperCase isnt applied to numbers, so
"123a4 1b234".toUpperCase()
works as expected.
You just need String.prototype.toUpperCase()
var myString = "123a4 1b234";
console.log(myString.toUpperCase());
I have something I am trying to accomplish.
I'd like to take an array built with AJAX/xml.
array[/word0/, /word1/, /word2/]
and put this into a form that could be used in a .match():
result = string.match(array)
I have tried using a for loop and stepping through the array using string.match(array[i]) to no avail.
Is there an easy way to do this?
Edit: You may have a syntax problem. The following is not valid syntax:
array[/word0/, /word1/, /word2/]
Something like this fixes it:
var regexps = [/word0/, /word1/, /word2/];
Original answer:
Javascript RegExps already do this. You're looking for:
var regexp = /word0|word1|word2/;
Assuming your list of matches comes back in the right format, you could achieve this like so:
var words = ["word0", "word1", "word2"];
var regexp = new Regexp(words.join("|"));
str.match(regexp);
http://jsfiddle.net/KALPh/
Your approach was fine. Here's my implementation:
var regexes = [/^def/, /^abc/],
testString = 'abcdef',
numRegexes = regexes.length;
for(var x=0;x<numRegexes;x++) {
alert(regexes[x].test(testString));
}
To initialize your array, use
var array = [/word0/, /word1/, /word2/];
Then you can use
str.match(array[i])
If your problem is the transmission in "AJAX/xml", then you'll need to build the regular expressions client side with new RegExp(somestring) where somestring might for example be "word0" : you can't embed a regex literal in XML.
How can I use jquery on the client side to substring "nameGorge" and remove "name" so it outputs just "Gorge"?
var name = "nameGorge"; //output Gorge
No jQuery needed! Just use the substring method:
var gorge = name.substring(4);
Or if the text you want to remove isn't static:
var name = 'nameGorge';
var toRemove = 'name';
var gorge = name.replace(toRemove,'');
Using .split(). (Second version uses .slice() and .join() on the Array.)
var result = name.split('name')[1];
var result = name.split('name').slice( 1 ).join(''); // May be a little safer
Using .replace().
var result = name.replace('name','');
Using .slice() on a String.
var result = name.slice( 4 );
Standard javascript will do that using the following syntax:
string.substring(from, to)
var name = "nameGorge";
var output = name.substring(4);
Read more here: http://www.w3schools.com/jsref/jsref_substring.asp
That's just plain JavaScript: see substring and substr.
You don't need jquery in order to do that.
var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);
var name = "nameGorge";
name.match(/[A-Z].*/)[0]
Yes you can, although it relies on Javascript's inherent functionality and not the jQuery library.
http://www.w3schools.com/jsref/jsref_substr.asp
The substr function will allow you to extract certain parts of the string.
Now, if you're looking for a specific string or character to use to find what part of the string to extract, you can make use of the indexOf function as well.
http://www.w3schools.com/jsref/jsref_IndexOf.asp
The question is somewhat vague though; even just link text with 'name' will achieve the desired result. What's the criteria for getting your substring, exactly?
How about the following?
<script charset='utf-8' type='text/javascript'>
jQuery(function($) { var a=$; a.noConflict();
//assumming that you are using an input text
// element with the text "nameGorge"
var itext_target = a("input[type='text']:contains('nameGorge')");
//gives the second part of the split which is 'Gorge'
itext_target.html().split("nameGorge")[1];
...
});
</script>
Is there another a way to concatenate a '#' character like I'm doing below?
radioButtonID = '#' + radioButtonID;
"#".concat(radioButtonID)
or
["#",radioButtonID].join('')
I suppose you could do something like this:
var radioButtonID = ['#', radioButtonID].join('');
That's about as short as it could get if you are prefixing.
What's wrong with what you've got there? That should work.
Unless you're looking for a stringbuffer or something...
Not very efficient, but this works:
radioButtonID.replace(/(.+)/,"#$1")