Hi firstly ty for looking at my code ^^
i have made a working example that hides the content based on the input but the problem is it looks letter for letter so lets say i have this text in my div "yes i know" i have yo start typing with the y to find it because if i type lets say "know" it wont find it
here is the example demo
and here is my code
I would like it to work that it would look for words in stead exact letter it starts
tyvm in advance ^^
$('#my-textbox').keyup(function() {
var value = $(this).val();
var exp = new RegExp('^' + value, 'i');
$('.panel-group .panel').each(function() {
var isMatch = exp.test($('.accordion-toggle', this).text());
$(this).toggle(isMatch);
});
});
simply remove the ^ from the RegExp. this means it has to start with said string
var exp = new RegExp(value, 'i');
On this link you can find more info about RegExp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
Related
Hi,
I have this code:
var room = 'room2';
var exitroom = 'room1,room2,room3';
exitroom = exitroom.replace(/,${room},/,'');
console.log(exitroom);
you can try it here: https://jsfiddle.net/uq9w0Ls4/
my expected output is simply room1,room3 by taking room2 out but since it may change its position within the string I want to target the , no matter if it comes before or after the string but I cant figure out the regex logic here. I know I could just do simply:
var room = 'room2';
var exitroom = 'room1,room2,room3';
exitroom = exitroom.replace(room+',','').replace(','+room,'');
console.log(exitroom);
which works but I think regex would be a more direct approach.
Thank you.
First, by writing .replace(/,${room},/,'') you are not using the variable room.
To use a variable in a regex you should call new RegExp()
Second, if you want a regex that will match when the comma is before or after the word, you can use a group () with an Or | operator.
so it should look like this:
var reg = new RegExp(`(?:${room},|,${room})`, "g");
exitroom.replace(reg,'');
The ?: at the beginning of the group, is just so it should be a non-capturing group, it should work just fine also without it
I need to find all the words in a string array which start with m but not have m in the middle?
var arr = "Hello my mother! how is Ramy?";
I tried that:
var ragexp = new RegExp("\sm[a-z]*|^m[a-z]*", "g");
var test = regexp.test(arr); // test should be true if anything matched
i hope this will be useful for you:
\bm[^m]*?\b
I recommend this page for see more visual the expressions, just select the Flags(global, case insensitive):
enter link description here
I've created my own autocomplete feature and I've come across a bug I'd like to fix. Here's an example of an incomplete sentence I might want to autocomplete the final word for:
let text = 'Hello there, I am her'
In my functionality the user clicks ctrl + enter and it autcompletes the word with a suggestion displayed on the page. In this case let's say the suggestion is 'here'. Also my controller knows where the user is based on the insertion cursor (so I have the index).
If I use replace like so:
text.replace(word, suggestion);
(Where word is 'her' and suggestion is 'here') it will replace the first occurrence. Obviously there are endless combinations of where this word might be in the text, how do I replace one at a certain index in text string? I know I can do it through some messy if conditions, but is there an elegant way to do this?
(If it is relevant I am using angular keydown/keyup for this)
EDIT>>>>>
This is not a duplicate on the question linked as in that case they are always replacing the last occurrence. If I did that then my program wouldn't support a user going back in their sentence and attempting to autocomplete a new word there
So, you have a position in a string and a number of characters to replace (=length of an incomplete word). In this case, would this work?
let text = 'appl and appl and appl'
function replaceAt(str, pos, len, replace) {
return str.slice(0, pos) + replace + str.slice(pos + len);
}
console.log(replaceAt(text, 0, 4, 'apple'))
console.log(replaceAt(text, 9, 4, 'apple'))
Gonna point you in a direction that should get you started.
let sentence = 'Hello lets replace all words like hello with Hi';
let fragments = sentence.split(' ');
for (let i=0; i<fragments.length; i++){
if(fragments[i].toLowerCase() == 'hello')
fragments[i] = 'Hi'
}
let formattedsentence = fragments.join(' ');
console.log(formattedsentence); //"Hi lets replace all words like Hi with Hi"
Im stumped. Im trying to work out how I can use javascript to find all instances of 'ipad' or 'iphone' on a page and then force each one to use a lowercase 'i' so they appear iPad or iPAD?
What you need is a regex that first checks for the letter i, then checks that the letter i is followed by either phone or pad:
var appleRegex = /i(?=(phone)|(pad))/gim;
You can then use replace() to replace any i character that matches this regex with a lowercase i:
var theText = 'IPAD and IPHONE';
var replacedText = theText.replace(appleRegex, 'i');
In the above, theText is the string you want to check against, and replacedText is the same string after both words are made to use a lowercase i.
I've created a fiddle demonstrating this here.
EDIT
To alter HTML content, you'll need to set up theText in the example to equal the contents of the HTML you wish to replace:
var theText = document.getElementById('{YOURID}').innerHTML;
Then you would need to update the element after replacing the text:
document.getElementById('{YOURID}').innerHTML = replacedText;
I've updated the Fiddle to showcase this, and the update can be found here.
Hope this helps!
When i have string which consists of a single line, replace works just fine.
As soon as i type in some text into text area and press enter as for new line, replace won't work anymore.
var currentValue = $('#service-field').val();
$('#service-field').val(currentValue.replace("particular string",""));
What should I do?
Try this to make sure you capture all occurrences, and not just the ones on the first line:
$('#service-field').val(currentValue.replace(/particular string/g, ""));
Or with a variable:
var t = "particular string";
$('#service-field').val(currentValue.replace(eval("/" + t + "/g"), ""));