Is there a way of combining these two patterns? I would like to remove spaces and non-alphanumeric characters.
This does work, it just seems inefficient repeating the replace function.
var str;
str = $('p').text().replace(/[^a-zA-Z 0-9]+/g, '');
str = str.replace(/\s/g, '');
alert(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>What's Happening Here!</p>
Example jsFiddle.
You can combine them using the 'or' operator (|), like this: /[^a-zA-Z 0-9]+|\s/g
var str = $('p').text().replace(/[^a-zA-Z 0-9]+|\s/g, '');
console.log(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>What's Happening Here!</p>
You could explicity only allow numbers and letters. This will discard any white space
str = $('p').text().replace(/[^0-9a-zA-Z]/g, '');
https://jsfiddle.net/fjnc8x4g/1/
ypu also add .replace after last like
str = $('p').text().replace(/[^a-zA-Z 0-9]+/g, '').replace(/\s/g, '');
var str;
str = $('p').text().replace(/[^a-zA-Z 0-9]+/g, '').replace(/\s/g, '');
alert(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<p>
What 's Happening Here!
</p>
Related
Hi there how can I replace from this to this
var str = document.getElementById('bos').innerHTML.replace('col_nr', "");
document.getElementById('bos').innerHTML = str;
<div id="bos">
col_nr[504]
</div>
I want to be able to take only the number without brackets
You can perform more replace() to achieve your goal, demonstrated as below. Alternatively, you can use regular expression to perform your task as well.
var str = document.getElementById('bos').innerHTML.replace('col_nr[', '').replace(']', '');
document.getElementById('bos').innerHTML = str;
<div id="bos">
col_nr[504]
</div>
You could replace all not number characters.
var element = document.getElementById('bos');
element.innerHTML = element.innerHTML.replace(/\D/g, "");
<div id="bos">
col_nr[504]
</div>
Is there a way to replace/remove the text only after a certain character using jQuery or Javascript? I want to remove text after the dot '.' from an element.
You can easily do it with .split() like this:
var text = 'daslkdaskldj.asdasdasd';
text.split('.')[0];
here is fiddle
var string = "Test String.Test String 2".split('.')[0];
console.log(string)
Will give you the output:
Test String
Here is a working example:
https://jsfiddle.net/zr2wg90d/
Your question is a bit unclear. But to remove all text after the first '.'(dot) This can do the trick with an input field. There are a lot of ways to achieve this. This is a solution without jQuery.
function removeAfterDot() {
var test = document.getElementById("myInput").value;
alert("String before remove: " + test);
test = test.substr(0, test.indexOf('.'));
alert("String after remove: " + test);
}
<input type="text" id="myInput" onchange=removeAfterDot();>
text.substr(0, text.indexOf('.'));
Hope this helps.
var q = 'https://stackoverflow.com/questions/';
q = q.substring(0, q.indexOf('.'));
alert(q);
Try this
var yourString = "Hello. World";
yourString.substr(0, yourString.indexOf('.'));
Will give you the following output
Hello
you can use this. split any string at the character you give it.
<p>first part . second part</p>
remove
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$('a').click(function(){
var the_string = $('p').text();
var removed = the_string.split('.', 1);
$('p').text(removed);
});
</script>
for me splice works, I basically use this for removing characters after a hyphen or a comma etc.
var text = 'Tellme.more';
text.split('.')[0]);
//Consoles out -> Tellme
This question already has answers here:
Matching quote wrapped strings in javascript with regex
(3 answers)
Closed 2 years ago.
I have a question, how can add <span style="color: blue"> to text in quotes.
Example:
.. and he said "Hello, I am Nick"
Using regex I want to achieve this result:
.. and he said <span style="color: blue>"Hello, I am Nick"</span>
I want to know how I can do that with regular expressions. Goal is to apply color only to text inside the quotes.
Using .replaceWith() function you can add span tag between any text with quotes.
$(document).ready(function() {
$("h2"). // all p tags
contents(). // select the actual contents of the tags
filter(function(i,el){ return el.nodeType === 3; }). // only the text nodes
each(function(i, el){
var $el = $(el); // take the text node as a jQuery element
var replaced = $el.text().replace(/"(.*?)"/g,'<span class="smallcaps">"$1"</span>') // wrap
$el.replaceWith(replaced); // and replace
});
});
.smallcaps {
color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2>and he said "Hello, i am Nick" and "I am good"</h2>
Use String.prototype.replace() method:
var str = document.querySelector('div').textContent;
var reg = /(".*\")+/g
var s = str.replace(reg, function(m){
return '<span style="color:blue">'+m+'</span>';
})
document.querySelector('div').innerHTML = s;
<div>and he said "Hello, I am Nick", some extra</div>
You can use the String's .replace() function as follows:
(1) If you want to keep the quotes and have them inside the <span>:
var source = '---- "xxxx" ---- "xxxx" ----';
var result = source.replace(/"[^"]*"/g, '<span style="color:blue">$&</span>');
console.log(result);
$('#container').html(result);
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div id="container"></div>
Notes:
The [^"] sequence in the regular expression defines a set of characters that matches all characters other than a double quote. Therefore, [^"]* matches zero or more characters that are not a double quote.
The $& in the replacement string will be replaced with the matched characters.
(2) If you do not want to keep the quotes:
var source = '---- "xxxx" ---- "xxxx" ----';
var result = source.replace(/"([^"]*)"/g, '<span style="color:blue">$1</span>');
console.log(result);
$('#container').html(result);
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div id="container"></div>
The parentheses in the regular expression create a capturing group. (Notice that the quotes are not within the capturing group.)
The $1 in the replacement string will be replaced with the first capturing group.
(3) If you want to keep the quotes, but have them outside the <span>:
var source = '---- "xxxx" ---- "xxxx" ----';
var result = source.replace(/"([^"]*)"/g, '"<span style="color:blue">$1</span>"');
console.log(result);
$('#container').html(result);
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div id="container"></div>
Note: This is the same as #2, but the quotes are included in the substitution string, so they are put back in the result string.
If regex is not mandatory, then try this split-map-join as well
var text = document.getElementById( "el" ).innerHTML;
function transform(input)
{
return input.split("\"").map( function(item,index){ if( index % 2 != 0 ){ item = '<span style="color: blue">' + item; } return item }).join("");
}
document.getElementById( "el" ).innerHTML = transform(text)
<div id="el">
and he said "Hello, i am Nick"
</div>
'and he said "Hello, I am Nick"'.replace(/"Hello, I am Nick"/, '<span style="color: blue">$&</span>');
According to this page this should work. Here is the code and the JSFiddle.
<input id="id.docType" value="45"/>
<br/>
<p></p>
<input id="thevalue" />
var str = 'id.docType';
str = str.replace('.', '\\\\.');
var selector = '#' + str;
$('p').text(selector);
var x = $(selector).val();
$('#thevalue').val(x);
Any ideas why this doesn't work? I have ids that have periods and trying to use them as a selector with jQuery. jQuery's page says I should be able to escape the period with 2 back slashes but it isn't working.
Change
str = str.replace('.', '\\\\.');
to
str = str.replace('\.', '\\.');
jsFiddle example
The slash is double escaped, it only needs escaped once:
str = str.replace('.', '\\.');
I'm a newbie in using RegEX and I could really use some help.
I'm doing some string replacements and I currently get the output
<div /foo>
Instead of
</div>
str = "[foo][/foo]";
Regex used:
str= str.replace(/\[/g, '<div ').replace(/\]/g, '>');
Output wanted:
<div foo></div>
Can someone help me to replace the string in the right way?
Thank you very much!
Not much content to your question so I just posted something which gets the job done. Note this assumes you do not care about anything after the opening tag, it only keeps the name of the tag and replaces it by </tagname>.
var str = "<div /foo>";
var replaced = str.replace(/<(\w+).*/, '</$1>')
// "</div>"
This one could suit your needs:
\[([^\]]+)\](.*?)\[/\1\]
Replace with: <div $1>$2</div>
Visualization by Debuggex
Demo on RegExr
PS: don't forget to escape the / if using JavaScript's regex literal, i.e.:
/\[([^\]]+)\](.*?)\[\/\1\]/g
How about:
str= str.replace(/\[(.+?)\]\[\/\1\]/g, '<div $1></div>');
You can do like this also.
var str = "[foo][/foo]";
//replace <, > with [, ]
var signReplace = str.replace(/\[/g, '<').replace(/\]/g, '>');
tagReplace = signReplace.replace(/foo/g, 'div'); //replace div with foo