Hei all
I have this code
function prototype( str , id )
{
var ret = str;
ret = ret.replace( /ø/g, 'oe' );
ret = ret.replace( /Ø/g, 'OE' );
ret = ret.replace( /å/g, 'aa' );
ret = ret.replace( /Å/g, 'AA' );
ret = ret.replace( /æ/g, 'ae' );
ret = ret.replace( /Æ/g, 'AE' );
document.getElementById( id ).value = ret.replace(/[^a-zA-Z0-9\/\_-]/i,'_').replace(/_+/g,'_');
}
My problem is now, if i use word like this (demo demo demo) its okay make this word to (demo_demo demo)
i use this function to escape urls. the next i need its send it to lower case, after I'm done, i hope for help :)
tanks a lot all.
You forgot the greedy-modifier
/[^a-zA-Z0-9\/\_-]/i
....
/[^a-zA-Z0-9\/\_-]/ig
If the problem is that only the first space gets replaced with _, then you need to put the g option to the regex replace.
ret.replace(/[^a-zA-Z0-9\/\_-]/gi,'_')
to turn the string to lower case use the
toLowerCase() method of strings.
Assuming that your 1st requirement is to replace space, +, - characters with underscore,
document.getElementById( id ).value = ret.replace(/[^a-zA-Z0-9\/\_-]/ig,'_').replace(/_+/ig,'_');
Other requirement is to make it lowercase string
document.getElementById( id ).value = document.getElementById( id ).value.toLowerCase();
The main potential issue I see, is on first replacement (on Ø and other scandinavian characters).
you should change with unicode char representation, e.g. this statement
ret = ret.replace( /Ø/g, 'OE' );
should be
ret = ret.replace( /\u0153/g, 'OE' );
for other diacritic signs just find a unicode chart like http://www.chucke.com/entities.html
Related
I have a table with 7 columns, I am getting the value of the last column:
var chkValue = $(this).closest('th').next('td').next('td').next('td').next('td').next('td').next('td').next('td').text();
For example the value is: "TEST"
When I try:
alert(chkValue);
return
TEST
How I can remove this characters?
You can replace characters that are not word characters. Though root issue appears to be the encoding of the document.
chkValue = chkValue.replace(/[^\w]/g, "");
The replace is the way to go, but you have to know what characters are that...
I just wanted to give you a piece of advice, to simplifying your selection and make it independent of the number of cols (maybe in the future you have to add more columns and don't want to break this) you can use...
var chkValue = $(this).closest('th').children('td').last().text();
This is assuming that what you see as are all the same characters (they may all look like squares but might be different unicode characters)
var myElement = $(this).closest('th').next('td').next('td').next('td').next('td').next('td').next('td').next('td');
var newValue = myElement.text().replace('', '');
myElement.text( newValue );
you can use regex
try this:
"TEST".replace(/\W/g, '');
which \W is words and numbers
specifically for you add:
chkValue = chkValue.replace(/\W/g, '');
If you are using HTML5 then define charset. It might help you resolve the issue.
<head>
<meta charset="UTF-8">
</head>
Try this
var unicodeVal = "".charCodeAt(0); //get unicode value first
var output = "TEST".replace( /./g, function(match) { return match.charCodeAt(0) == unicodeVal ? "" : match } );
console.log( output );
//simpler approach if there is only one character to be replaced
output = "TEST".replace( /./g, function(match) { return match == "" ? "" : match } )
console.log( output );
How can you remove letters from a string in jquery
So for example if you had the following
var gif = "example_one.gif";
how could i out put it so it would show
"example.gif"
So remove the last four characters but keep the extension?
Regex approach
- Removes anything and including the underscore up until the extension
var gif = "example_one.gif";
gif = gif.replace(/(?=_).*(?=\.)/g,'');
DEMO
Explanation here
(?=_) Positive Lookahead - Assert that "underscore" can be matched
.* Matches any character (except newline)
(?=\.) Positive Lookahead - Assert that "period" can be matched
g modifier: Global. All matches (don't return on first match)
that what you want?
var gif = "example_one.gif" ;
gif = gif.substr(0, gif.indexOf("_")) + gif.substr(gif.indexOf("."), gif.length);
Walking through it the most basic way...
First find the .:
var gif = "example_one.gif";
var end = gif.lastIndexOf(".")
Then split the string:
var name_only = gif.substring(0,end)
Then take out what you want taken out:
var trimmed = name_only.substring(0,name_only.length-5)
Then put your extension back:
var cleaned = trimmed + gif.substring(end-1,gif.length)
Check it:
alert( cleaned )
Working fiddle: http://jsfiddle.net/digitalextremist/G27HN/
Or do it with a reusable function! http://jsfiddle.net/digitalextremist/wNu8U/
WITH ability to change length of trim job needed:
function cleanEnding( named, count ) {
if ( count == undefined ) count = 4
var end = named.lastIndexOf( "." )
var name_only = named.substring( 0, end )
var trimmed = name_only.substring( 0, name_only.length - count-1 )
var cleaned = trimmed + named.substring( end-1, named.length )
return cleaned
}
//de You CAN pass in a number after this.
//de The function defaults to trimming out 4 before the extension.
alert( cleanEnding( "example_one.gif" ) )
If its always the last four characters before the extension (and the extension is three characters):
var gif = "example_one.gif";
var gif2 = gif.substring(0, gif.length - 8) + gif.substring(gif.length - 4);
console.log(gif2);
http://jsfiddle.net/2cYrj/
var gif = "example_one.gif";
var str = gif.split('.');
str[0] = str[0].slice(0, -4);
gif = str.join('.');
console.log(gif);
var parts = gif.split('.');
var newstring = parts[0].substr(0, parts[0].length-4) + "." + parts[1];
gif.replace('_one', '')
Does this help you or you want it more general?
try this: gif.replace("_one","");
I want to split a string in Javascript using split function into 2 parts.
For Example i have string:
str='123&345&678&910'
If i use the javascripts split, it split it into 4 parts.
But i need it to be in 2 parts only considering the first '&' which it encounters.
As we have in Perl split, if i use like:
($fir, $sec) = split(/&/,str,2)
it split's str into 2 parts, but javascript only gives me:
str.split(/&/, 2);
fir=123
sec=345
i want sec to be:
sec=345&678&910
How can i do it in Javascript.
var subStr = string.substring(string.indexOf('&') + 1);
View this similar question for other answers:
split string only on first instance of specified character
You can use match instead of split:
str='123&345&678&910';
splited = str.match(/^([^&]*?)&(.*)$/);
splited.shift();
console.log(splited);
output:
["123", "345&678&910"]
You can remain on the split part by using the following trick:
var str='123&345&678&910',
splitted = str.split( '&' ),
// shift() removes the first item and returns it
first = splitted.shift();
console.log( first ); // "123"
console.log( splitted.join( '&' ) ); // "345&678&910"
I wrote this function:
function splitter(mystring, mysplitter) {
var myreturn = [],
myindexplusone = mystring.indexOf(mysplitter) + 1;
if (myindexplusone) {
myreturn[0] = mystring.split(mysplitter, 1)[0];
myreturn[1] = mystring.substring(myindexplusone);
}
return myreturn;
}
var str = splitter("hello-world-this-is-a-test", "-");
console.log(str.join("<br>"));
//hello
//world-this-is-a-test
The output will be either an empty array (not match) or an array with 2 elements (before the split and everything after)
Demo
I have that:
var str='123&345&678&910';
str.split('&',1).concat( str.split('&').slice(1).join('&') );
//["123", "345&678&910"]
str.split('&',2).concat( str.split('&').slice(2).join('&') );
//["123", "345", "678&910"];
for convenience:
String.prototype.mySplit = function( sep, chunks) {
chunks = chunks|=0 &&chunks>0?chunks-1:0;
return this.split( sep, chunks )
.concat(
chunks?this.split( sep ).slice( chunks ).join( sep ):[]
);
}
What about the use of split() and replace()?:
Given we have that string str='123&345&678&910' We can do
var first = str.split("&",1); //gets the first word
var second = str.replace(first[0]+"&", ""); //removes the first word and the ampersand
Please note that split() returns an array that is why getting the index with first[0] is recommended, however, without getting the index, it still worked as needed i.e first+"&".
Feel free to replace the "&" with the string you need to split with.
Hope this helps :)
How can I form a regular expression that match the unique numbers that repeat in a repeating decimals?
Currently my regular expressions is the following.
var re = /(?:[^\.]+\.\d*)(\d+)+(?:\1)$/;
Example:
// Pass
deepEqual( func(1/111), [ "0.009009009009009009", "009" ] );
// Fails, since func(11/111) returns [ "0.099099099099099", "9" ]
deepEqual( func(11/111), [ "0.099099099099099", "099" ] );
Live demo here: http://jsfiddle.net/9dGsw/
Here's my code.
// Goal: Find the pattern within repeating decimals.
// Problem from: Ratio.js <https://github.com/LarryBattle/Ratio.js>
var func = function( val ){
var re = /(?:[^\.]+\.\d*)(\d+)+(?:\1)$/;
var match = re.exec( val );
if( !match ){
val = (val||"").toString().replace( /\d$/, '' );
match = re.exec( val );
}
return match;
};
test("find repeating decimals.", function() {
deepEqual( func(1), null );
deepEqual( func(1/10), null );
deepEqual( func(1/111), [ "0.009009009009009009", "009" ] );
// This test case fails...
deepEqual( func(11/111), [ "0.099099099099099", "099" ],
"What's wrong with re in func()?" );
deepEqual( func(100/111), [ "0.9009009009009009", "009"] );
deepEqual( func(1/3), [ "0.3333333333333333", "3"]);
});
Ok. I somewhat solved my own problem by taking Joel's advice.
The problem was that the regular expression section, (\d+)+(?:\1)$, was matching the pattern closest to the end of the string, which made it return "9", instead of "099" for the string "0.099099099099099".
The way I overcame this problem was by setting the match length to 2 or greater, like so.
(\d{2,})+(?:\1)$,
and filtering the result with /^(\d+)(?:\1)$/, incase that a pattern is stuck inside a pattern.
Here's the code that passes all my test cases.
Live Demo: http://jsfiddle.net/9dGsw/1/
var func = function( val ){
val = (val || "").toString();
var RE_PatternInRepeatDec = /(?:[^\.]+\.\d*)(\d{2,})+(?:\1)$/,
RE_RepeatingNums = /^(\d+)(?:\1)$/,
match = RE_PatternInRepeatDec.exec( val );
if( !match ){
// Try again but take off last digit incase of precision error.
val = val.replace( /\d$/, '' );
match = RE_PatternInRepeatDec.exec( val );
}
if( match && 1 < match.length ){
// Reset the match[1] if there is a pattern inside the matched pattern.
match[1] = RE_RepeatingNums.test(match[1]) ? RE_RepeatingNums.exec(match[1])[1] : match[1];
}
return match;
};
Thank you for everyone that helped.
Use: var re = /^(?:\d*)\.(\d{1,3})(?:\1)+$/
I have defined the min/max length with {min,max} of the repeating decimal because otherwise 009009009 would match in the first test case as well. Maybe it is still not the final solution, but at least a hint.
Given a string
'1.2.3.4.5'
I would like to get this output
'1.2345'
(In case there are no dots in the string, the string should be returned unchanged.)
I wrote this
function process( input ) {
var index = input.indexOf( '.' );
if ( index > -1 ) {
input = input.substr( 0, index + 1 ) +
input.slice( index ).replace( /\./g, '' );
}
return input;
}
Live demo: http://jsfiddle.net/EDTNK/1/
It works but I was hoping for a slightly more elegant solution...
There is a pretty short solution (assuming input is your string):
var output = input.split('.');
output = output.shift() + '.' + output.join('');
If input is "1.2.3.4", then output will be equal to "1.234".
See this jsfiddle for a proof. Of course you can enclose it in a function, if you find it necessary.
EDIT:
Taking into account your additional requirement (to not modify the output if there is no dot found), the solution could look like this:
var output = input.split('.');
output = output.shift() + (output.length ? '.' + output.join('') : '');
which will leave eg. "1234" (no dot found) unchanged. See this jsfiddle for updated code.
It would be a lot easier with reg exp if browsers supported look behinds.
One way with a regular expression:
function process( str ) {
return str.replace( /^([^.]*\.)(.*)$/, function ( a, b, c ) {
return b + c.replace( /\./g, '' );
});
}
You can try something like this:
str = str.replace(/\./,"#").replace(/\./g,"").replace(/#/,".");
But you have to be sure that the character # is not used in the string; or replace it accordingly.
Or this, without the above limitation:
str = str.replace(/^(.*?\.)(.*)$/, function($0, $1, $2) {
return $1 + $2.replace(/\./g,"");
});
You could also do something like this, i also don't know if this is "simpler", but it uses just indexOf, replace and substr.
var str = "7.8.9.2.3";
var strBak = str;
var firstDot = str.indexOf(".");
str = str.replace(/\./g,"");
str = str.substr(0,firstDot)+"."+str.substr(1,str.length-1);
document.write(str);
Shai.
Here is another approach:
function process(input) {
var n = 0;
return input.replace(/\./g, function() { return n++ > 0 ? '' : '.'; });
}
But one could say that this is based on side effects and therefore not really elegant.
This isn't necessarily more elegant, but it's another way to skin the cat:
var process = function (input) {
var output = input;
if (typeof input === 'string' && input !== '') {
input = input.split('.');
if (input.length > 1) {
output = [input.shift(), input.join('')].join('.');
}
}
return output;
};
Not sure what is supposed to happen if "." is the first character, I'd check for -1 in indexOf, also if you use substr once might as well use it twice.
if ( index != -1 ) {
input = input.substr( 0, index + 1 ) + input.substr(index + 1).replace( /\./g, '' );
}
var i = s.indexOf(".");
var result = s.substr(0, i+1) + s.substr(i+1).replace(/\./g, "");
Somewhat tricky. Works using the fact that indexOf returns -1 if the item is not found.
Trying to keep this as short and readable as possible, you can do the following:
JavaScript
var match = string.match(/^[^.]*\.|[^.]+/g);
string = match ? match.join('') : string;
Requires a second line of code, because if match() returns null, we'll get an exception trying to call join() on null. (Improvements welcome.)
Objective-J / Cappuccino (superset of JavaScript)
string = [string.match(/^[^.]*\.|[^.]+/g) componentsJoinedByString:''] || string;
Can do it in a single line, because its selectors (such as componentsJoinedByString:) simply return null when sent to a null value, rather than throwing an exception.
As for the regular expression, I'm matching all substrings consisting of either (a) the start of the string + any potential number of non-dot characters + a dot, or (b) any existing number of non-dot characters. When we join all matches back together, we have essentially removed any dot except the first.
var input = '14.1.2';
reversed = input.split("").reverse().join("");
reversed = reversed.replace(\.(?=.*\.), '' );
input = reversed.split("").reverse().join("");
Based on #Tadek's answer above. This function takes other locales into consideration.
For example, some locales will use a comma for the decimal separator and a period for the thousand separator (e.g. -451.161,432e-12).
First we convert anything other than 1) numbers; 2) negative sign; 3) exponent sign into a period ("-451.161.432e-12").
Next we split by period (["-451", "161", "432e-12"]) and pop out the right-most value ("432e-12"), then join with the rest ("-451161.432e-12")
(Note that I'm tossing out the thousand separators, but those could easily be added in the join step (.join(','))
var ensureDecimalSeparatorIsPeriod = function (value) {
var numericString = value.toString();
var splitByDecimal = numericString.replace(/[^\d.e-]/g, '.').split('.');
if (splitByDecimal.length < 2) {
return numericString;
}
var rightOfDecimalPlace = splitByDecimal.pop();
return splitByDecimal.join('') + '.' + rightOfDecimalPlace;
};
let str = "12.1223....1322311..";
let finStr = str.replace(/(\d*.)(.*)/, '$1') + str.replace(/(\d*.)(.*)/, '$2').replace(/\./g,'');
console.log(finStr)
const [integer, ...decimals] = '233.423.3.32.23.244.14...23'.split('.');
const result = [integer, decimals.join('')].join('.')
Same solution offered but using the spread operator.
It's a matter of opinion but I think it improves readability.