Version compl�te: jsB@nk » Divers » Rechercher et remplacer l'aide de split () et join ()
URL: https://www.javascriptbank.com/find-and-replace.html
Vous avez probablement vu d'autres find
et de remplacer les scripts qui utilisent des objets de gamme ou à TextRange find
et remplacer du texte, ou indexOf () et substring () pour manipuler le texte, mais il s'agit d'une troisième façon de faire, qui est par la division () et join () méthodes.
Version compl�te: jsB@nk » Divers » Rechercher et remplacer l'aide de split () et join ()
URL: https://www.javascriptbank.com/find-and-replace.html
<SCRIPT>/*Find and Replace using split() and join()by pile o' nadesThis script will find and replace */function FindReplace(find,replaceWith){ // get value of find and replaceWith textboxes find = document.getElementById(find).value; replaceWith = document.getElementById(replaceWith).value; // get HTML of search area var result = document.getElementById("searchField").innerHTML; // split the search area into array items result = result.split(find); // get number of items while result is still an array var numberOfItems = result.length-1; // join the array items back into a string using replaceWith result = result.join(replaceWith); // rewrite the search area with the new text document.getElementById("searchField").innerHTML = result; // write number of items to the number textbox document.getElementById("number").value = numberOfItems;}</SCRIPT><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->
<DIV style="text-align: left;"><P>Normally, the split() and join() methods are used to combine arrays into strings, to be stored in cookies. But they can also be used for a find and replace script, such as this one. </P><P>The split() method takes a peice of text and/or HTML, and makes each peice of text before and after each occurance of the text into an array item. <XMP>Example: var text = "Congress shall make no law respecting an establishment of religion"; text = text.split("a") This would equal: var text = new Array(); text[0]="Congress shall make no l"; text[1]="w respecting "; text[2]="n est"; text[3]="blishment of religion"; all of the "a"s have been removed </XMP><P></P><P>The join() method does the opposite, it recontructs the array into a string <XMP>Example: text = text.join("a") This would equal: var text = "Congress shall make no law respecting an establishment of religion"; However, you can use a different character for join() Example: text = text.join("117") This would equal: var text = "Congress shall make no l117w respecting 117n est117blishment of religion";</XMP><P></P><P>That's how this script works. Try it using the form below. </P><P align=right><FORM>Find:<INPUT id=find value=law size="20"><BR>Replace with:<INPUT id=replaceWith value=legislation size="20"><BR>Number of items:<INPUT id=number size="20"><BR><BUTTON id=button onclick="FindReplace('find','replaceWith')">Go!</BUTTON> </FORM></P></DIV><HR><DIV id=searchField style="text-align: left;"><!-- Bill of Rights --><H2><A id=article1 name=article1>ARTICLE I</A></H2><P>Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.</P><H2><A id=article2 name=article2>ARTICLE II</A></H2><P>A well regulated militia, being necessary to the security of a free state, the right of the people to keep and bear arms, shall not be infringed.</P><H2><A id=article3 name=article3>ARTICLE III</A></H2><P>No soldier shall, in time of peace, be quartered in any house, without the consent of the owner, nor in time of war, but in in a manner to be prescribed by law.</P><H2><A id=article4 name=article4>ARTICLE IV</A></H2><P>The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.</P><H2><A id=article5 name=article5>ARTICLE V</A></H2><P>No person shall be held to answer for a capital, or otherwise infamous crime, unless on a presentment or indictment of a grand jury, except in cases arising in the land or naval forces, or in the militia, when in actual service in time of war or public danger; nor shall any person be subject for the same offense to be twice put in jeopardy of life or limb; nor shall be compelled in any criminal case to be a witness against himself, nor be deprived of life, liberty, or property, without due process of law; nor shall private property be taken for public use, without just compensation.</P><H2><A id=article6 name=article6>ARTICLE VI</A></H2><P>In all criminal prosecutions the accused shall enjoy the right to a speedy and public trial, by an impartial jury of the state and district wherein the crime shall have been committed, which district shall have been previously ascertained by law, and to be informed of the nature and cause of the accusation; to be confronted with the witnesses against him; to have compulsory process for obtaining witnesses in his favor, and to have the assistance of counsel for his defense.</P><H2><A id=article7 name=article7>ARTICLE VII</A></H2><P>In suits at common law, where the value in controversy shall exceed twenty dollars, the right of trial by jury shall be preserved, and no fact tried by a jury shall be otherwise re-examined in any court of the United States, than according to the rules of the common law.</P><H2><A id=article8 name=article8>ARTICLE VIII</A></H2><P>Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted.</P><H2><A id=article9 name=article9>ARTICLE IX</A></H2><P>The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.</P><H2><A id=article10 name=article10>ARTICLE X</A></H2><P>The powers not delegated to the United States by the Constitution, nor prohibited by it to the states, are reserved to the states respectively, or the the people.</P><HR></DIV><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->