This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I trim a string in javascript?
I need to remove only the last and the first spaces from a generic string.
Example:
var str = " hello world "; // become "hello world"
var str = "ehi, do you come from ? "; // become "ehi, do you come from ?"
var str = "I am from Cina"; // remain "I am from Cina"
How can I do that ?
The trim from jQuery is what you are looking for.
$.trim(' string with spaces at the ends ');
Or plain javascript:
' string with spaces at the ends '.trim()
Related
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
They send me a text file that starts with unnecessary information and then what is needed goes further. How to remove the beginning to a specific symbol.
Example: line = 'lots of text {text needed}';
It is necessary to delete everything before the symbol {. I tried the regular expression option below:
let str = /^[^{]+/.exec(line)[0];
but it returns the beginning of the text to the symbol {
I need to do the opposite
Thanks for any help
This is how you can do with JavaScript.
var str = 'lots of text {text needed}';
str = str.substring(str.indexOf("{") + 1);
console.log(str);
This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 4 years ago.
I have string like this:
var str = "Hello (World) I'm Newbie";
how to get World from string above using RegExp?, I'm sorry I don't understand about regex.
Thank's
Rather than using a regex - use .split()...Note the escaped characters in the splits. The first split gives "World) I'm Newbie" and the second gives "World".
var str = "Hello (World) I'm Newbie";
var strContent = str.split('\(')[1].split('\)')[0];
console.log(strContent); // gives "World"
Assuming that there will be atleast one such word, you can do it using String#match. The following example matches the words between parentheses.
console.log(
"Hello (World) I'm Newbie"
.match(/\(\w+\)/g)
.map(match => match.slice(1, -1))
)
This might help you for your regex
\w match whole world
+ plus with another regex
[] starts group
^ except
(World) matching word
var str = "Hello (World) I'm Newbie";
var exactword=str.replace(/\w+[^(World)]/g,'')
var filtered = str.replace(/(World)/g,'')
alert(exactword)
alert(filtered)
This question already has answers here:
how do I strip white space when grabbing text with jQuery?
(4 answers)
Closed 7 years ago.
How can I delete spaces before only text:
" with spaces between"
I need:
"some text with spaces between"
All I have found it's text.replace(/\s/g, '') but it doesn't work well:
"sometextwithspacesbetween"
and indexOf(' ') + 25 also isn't good solve, because I have a different text and number of spaces before it.
If it possible, help me please
Set the reg exp to look for the beginning of the string
text.replace(/^\s+/, '')
You can use trim() in javascript and jQuery.trim(string) in jQuery. Both of them remove the whitespace from the beginning and end of a string.
var str = " some text with spaces between";
console.log(str);
console.log(str.trim());
console.log(jQuery.trim(str));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
This question already has answers here:
JavaScript regex pattern concatenate with variable
(3 answers)
Closed 7 years ago.
I have a regex to check if a string contains a specific word. It works as expected:
/\bword\b/.test('a long text with the desired word amongst others'); // true
/\bamong\b/.test('a long text with the desired word amongst others'); // false
But i need the word which is about to be checked in a variable. Using new RegExp does not work properly, it always returns false:
var myString = 'a long text with the desired word amongst others';
var myWord = 'word';
new RegExp('\b' + myWord + '\b').test(myString); // false
myWord = "among";
new RegExp('\b' + myWord + '\b').test(myString); // false
What is wrong here?
var myWord = 'word';
new RegExp('\\b' + myWord + '\\b')
You need to double escape the \ when building a regex from a string.
This is because \ begins an escape sequence in a string literal, so it never makes it to the regex. By doing \\, you're including a literal '\' character in the string, which makes the regex /\bword\b/.
This question already has answers here:
How to trim() white spaces from a string?
(19 answers)
Closed 9 years ago.
How to do Trim operation to remove space character in a text field in JavaScript?
in case you did mean Trim:
x.replace(/^\s*|\s*$/g,'');
A total dupe of this question:
What is the best way to trim() in javascript
Unless you didn't actually mean trim of course and in fact wanted all the spaces replaced ? In which case you just want:
var myString = "foo bar ddd";
myString = myString.replace(/ /g, "");
Try this in FF and Chrome
var myString = " some text ";
alert(myString.trim());
In IE first use this and call like the example above.
// Adding trim function to String object
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
from here http://goo.gl/L802W